diff --git a/internal/websocket/broadcast.go b/internal/websocket/broadcast.go index 929cd26..346808a 100644 --- a/internal/websocket/broadcast.go +++ b/internal/websocket/broadcast.go @@ -22,11 +22,14 @@ func SendPermanentBroadCastMessage() { // Iterate over all connected clients and broadcast the message for _, client := range Clients { - err := client.SendMessage(websocket.TextMessage, jsonMessage) - if err != nil { - // Remove client on error + if time.Now().Sub(client.LastPong) > 10*time.Second { + // Remove unresponsive client client.Conn.Close() delete(Clients, client.Conn.LocalAddr()) + helper.Logger.Info("Client timed out", "host", client.RealIP, "clients", len(Clients)) + } + err := client.SendMessage(websocket.TextMessage, jsonMessage) + if err != nil { helper.Logger.Info("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients)) } } diff --git a/pkg/models/client.go b/pkg/models/client.go index 802caf8..a4223a0 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -30,10 +30,7 @@ func (c *WebsocketClient) SendMessage(messageType int, data []byte) error { return nil }) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) - err := c.Conn.WriteMessage(websocket.PingMessage, nil) - if err != nil { - return err - } + c.Conn.WriteMessage(websocket.PingMessage, nil) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) return c.Conn.WriteMessage(messageType, data)