mirror of
https://github.com/wisplite/tether.git
synced 2026-05-01 06:22:41 -05:00
basic websocket handling and testing
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
function test() {
|
||||
const ws = new WebSocket("ws://localhost:8080/tether");
|
||||
ws.onmessage = (event) => {
|
||||
console.log(event.data);
|
||||
};
|
||||
ws.onopen = () => {
|
||||
console.log("Connected to server");
|
||||
ws.send(JSON.stringify({
|
||||
type: "subscribe",
|
||||
channel: "messages",
|
||||
}));
|
||||
};
|
||||
ws.onclose = () => {
|
||||
console.log("Disconnected from server");
|
||||
};
|
||||
}
|
||||
test();
|
||||
@@ -1,11 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/wisplite/tether"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `gorm:"primaryKey"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type Messages struct {
|
||||
ID string `gorm:"primaryKey"`
|
||||
Message string
|
||||
SenderID string
|
||||
ReceiverID string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func main() {
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
@@ -14,6 +31,9 @@ func main() {
|
||||
|
||||
engine := tether.NewEngine(db)
|
||||
|
||||
engine.CreateTable("users", &User{})
|
||||
engine.CreateTable("messages", &Messages{})
|
||||
|
||||
engine.RegisterMutation("createUser", func(ctx *tether.MutationCtx) error {
|
||||
return nil
|
||||
})
|
||||
@@ -21,4 +41,7 @@ func main() {
|
||||
engine.RegisterQuery("getUser", func(ctx *tether.QueryCtx) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
http.HandleFunc("/tether", engine.Handle)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user