try to check for last ping connect

This commit is contained in:
Sebastian Mark 2024-11-20 20:15:24 +01:00
parent 201e5779e6
commit 15f6b0227a
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
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))
}
}

View file

@ -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)