change db structure, add goroutine connection handling

This commit is contained in:
GitProtogen
2026-03-12 14:24:31 +01:00
parent afdc3f96a0
commit b7b5788f98
5 changed files with 79 additions and 101 deletions
+6 -15
View File
@@ -4,32 +4,23 @@ import (
"context"
"log"
"net/http"
"github.com/coder/websocket"
)
func main() {
InitDatabase(context.Background())
srv := &wsServer{
OnOpen: func(ctx context.Context, conn *websocket.Conn) {
OnOpen: func(c *Client) {
log.Println("client connected")
if getConnectionDataIfAuth(conn) != nil {
mu.Lock()
unauthenticatedConnections = append(unauthenticatedConnections, conn)
mu.Unlock()
}
},
OnClose: func(ctx context.Context, conn *websocket.Conn, err error) {
OnClose: func(c *Client, err error) {
log.Println("client disconnected:", err)
removeConnectionCache(conn)
},
OnMessage: func(ctx context.Context, conn *websocket.Conn, msg map[string]any) {
OnMessage: func(c *Client, msg map[string]any) {
log.Printf("received: %v\n", msg)
authConnOrNil := getConnectionDataIfAuth(conn)
if authConnOrNil == nil {
handleUnauthenticatedMessage(conn, msg)
if c.User == nil {
handleUnauthenticatedMessage(c, msg)
} else {
handleAuthenticatedMessage(conn, msg)
handleAuthenticatedMessage(c, msg)
}
},
}