add ping query
This commit is contained in:
parent
2d8816f4ba
commit
201e5779e6
1 changed files with 14 additions and 4 deletions
|
@ -18,13 +18,23 @@ type ClientCommand struct {
|
|||
// Represents a single client
|
||||
type WebsocketClient struct {
|
||||
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)
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue