diff --git a/pkg/models/client.go b/pkg/models/client.go index 3df5097..802caf8 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -17,14 +17,24 @@ type ClientCommand struct { // Represents a single client type WebsocketClient struct { - Conn *websocket.Conn - RealIP string + Conn *websocket.Conn + LastPong time.Time + RealIP string } // Sends a message to the websocket. // Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time. func (c *WebsocketClient) SendMessage(messageType int, data []byte) error { + c.Conn.SetPongHandler(func(appData string) error { + c.LastPong = time.Now() + return nil + }) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) - err := c.Conn.WriteMessage(messageType, data) - return err + err := c.Conn.WriteMessage(websocket.PingMessage, nil) + if err != nil { + return err + } + + c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) + return c.Conn.WriteMessage(messageType, data) }