change db structure, add goroutine connection handling
This commit is contained in:
@@ -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)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user