add ping query

This commit is contained in:
Sebastian Mark 2024-11-20 20:07:29 +01:00
parent 03d94d5999
commit df965e633d

View file

@ -17,14 +17,24 @@ type ClientCommand struct {
// Represents a single client // Represents a single client
type WebsocketClient struct { type WebsocketClient struct {
Conn *websocket.Conn Conn *websocket.Conn
RealIP string LastPong time.Time
RealIP string
} }
// Sends a message to the websocket. // Sends a message to the websocket.
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time. // 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 { 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)) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
err := c.Conn.WriteMessage(messageType, data) err := c.Conn.WriteMessage(websocket.PingMessage, nil)
return err if err != nil {
return err
}
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
return c.Conn.WriteMessage(messageType, data)
} }