fix: add more Mutex Locks for Clients map

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

View file

@ -22,6 +22,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)
@ -29,6 +30,7 @@ func SendPermanentBroadCastMessage() {
helper.Logger.Error("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients)) helper.Logger.Error("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
} }