From 201e5779e63d4fc89844443776991963644c5474 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 20 Nov 2024 20:07:29 +0100 Subject: [PATCH] add ping query --- pkg/models/client.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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) }