diff --git a/internal/websocket/broadcast.go b/internal/websocket/broadcast.go index f84c499..2decfb0 100644 --- a/internal/websocket/broadcast.go +++ b/internal/websocket/broadcast.go @@ -21,6 +21,7 @@ func SendPermanentBroadCastMessage() { } // Iterate over all connected clients and broadcast the message + mu.Lock() for _, client := range Clients { // Send message to client err := client.SendMessage(websocket.TextMessage, jsonMessage) @@ -28,6 +29,7 @@ func SendPermanentBroadCastMessage() { helper.Logger.Info("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients)) } } + mu.Unlock() <-tick.C } diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index 6f85108..7712d08 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -22,7 +22,9 @@ func handleClientCommands(c models.WebsocketClient) { _, message, err := ws.ReadMessage() if err != nil { // remove client on error/disconnect + mu.Lock() delete(Clients, ws.LocalAddr()) + mu.Unlock() helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients)) break }