basic websocket handling and testing

This commit is contained in:
wisplite
2026-04-07 12:01:42 -05:00
parent 5449dc7fe9
commit 27dc7818f7
4 changed files with 77 additions and 0 deletions
+17
View File
@@ -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();
+23
View File
@@ -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)
}