add ping query

This commit is contained in:
Sebastian Mark 2024-11-20 20:07:29 +01:00
parent 2d8816f4ba
commit 201e5779e6

View file

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