try to check for last ping connect

This commit is contained in:
Sebastian Mark 2024-11-20 20:15:24 +01:00
parent df965e633d
commit 9fa740b2c6
2 changed files with 7 additions and 7 deletions

View file

@ -22,11 +22,14 @@ func SendPermanentBroadCastMessage() {
// Iterate over all connected clients and broadcast the message // Iterate over all connected clients and broadcast the message
for _, client := range Clients { for _, client := range Clients {
err := client.SendMessage(websocket.TextMessage, jsonMessage) if time.Now().Sub(client.LastPong) > 10*time.Second {
if err != nil { // Remove unresponsive client
// Remove client on error
client.Conn.Close() client.Conn.Close()
delete(Clients, client.Conn.LocalAddr()) 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)) helper.Logger.Info("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients))
} }
} }

View file

@ -30,10 +30,7 @@ func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
return nil return nil
}) })
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
err := c.Conn.WriteMessage(websocket.PingMessage, nil) c.Conn.WriteMessage(websocket.PingMessage, nil)
if err != nil {
return err
}
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
return c.Conn.WriteMessage(messageType, data) return c.Conn.WriteMessage(messageType, data)