From 9fa740b2c6f645c98f9071a3d76c77c77629ea0c Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 20 Nov 2024 20:15:24 +0100 Subject: [PATCH] try to check for last ping connect --- internal/websocket/broadcast.go | 9 ++++++--- pkg/models/client.go | 5 +---- 2 files changed, 7 insertions(+), 7 deletions(-) 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)