update writePingMsg logic

This commit is contained in:
Monet Lee 2024-07-10 11:40:46 +08:00
parent 32457ab8b5
commit b454aacd36

View File

@ -347,7 +347,7 @@ func (c *Client) activeHeartbeat(ctx context.Context) {
for {
select {
case <-ticker.C:
if err := c.conn.WriteMessage(PingMessage, nil); err != nil {
if err := c.writePingMsg(); err != nil {
log.ZError(c.ctx, "send Ping Message error.", err)
return
}
@ -358,6 +358,21 @@ func (c *Client) activeHeartbeat(ctx context.Context) {
}()
}
}
func (c *Client) writePingMsg() error {
if c.closed.Load() {
return nil
}
c.w.Lock()
defer c.w.Unlock()
err := c.conn.SetWriteDeadline(writeWait)
if err != nil {
return err
}
return c.conn.WriteMessage(PingMessage, nil)
}
func (c *Client) writePongMsg() error {
if c.closed.Load() {