add write ping before pinghandler starts

This commit is contained in:
Adrian Zuercher
2025-07-27 09:05:53 +02:00
parent c3a3060129
commit b21101958d

View File

@@ -20,7 +20,7 @@ const (
writeWait = 30 * time.Second writeWait = 30 * time.Second
// Time allowed to read the next pong message from the peer. // Time allowed to read the next pong message from the peer.
pongWait = 30 * time.Second pongWait = 40 * time.Second
// Send pings to peer with this period. Must be less than pongWait. // Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10 pingPeriod = (pongWait * 9) / 10
@@ -162,6 +162,10 @@ func (c *Client) PingInterval(interval time.Duration) {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
defer ticker.Stop() defer ticker.Stop()
if err := c.conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(pongWait)); err != nil {
c.OnError(err)
return
}
for range ticker.C { for range ticker.C {
if c.OnPing != nil { if c.OnPing != nil {
c.OnPing() c.OnPing()