add more Mutex Locks for Clients map

This commit is contained in:
Sebastian Mark 2024-11-20 21:51:12 +01:00
parent bf9b818940
commit 0ab0884508
2 changed files with 4 additions and 0 deletions

View file

@ -21,6 +21,7 @@ func SendPermanentBroadCastMessage() {
} }
// Iterate over all connected clients and broadcast the message // Iterate over all connected clients and broadcast the message
mu.Lock()
for _, client := range Clients { for _, client := range Clients {
// Send message to client // Send message to client
err := client.SendMessage(websocket.TextMessage, jsonMessage) 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)) helper.Logger.Info("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients))
} }
} }
mu.Unlock()
<-tick.C <-tick.C
} }

View file

@ -22,7 +22,9 @@ func handleClientCommands(c models.WebsocketClient) {
_, message, err := ws.ReadMessage() _, message, err := ws.ReadMessage()
if err != nil { if err != nil {
// remove client on error/disconnect // remove client on error/disconnect
mu.Lock()
delete(Clients, ws.LocalAddr()) delete(Clients, ws.LocalAddr())
mu.Unlock()
helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients)) helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients))
break break
} }