File tree Expand file tree Collapse file tree 2 files changed +12
-1
lines changed
Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -49,7 +49,11 @@ func (c *WSClient) handleWSMessages() error {
4949 return errors .New ("连接已关闭" )
5050 }
5151
52- _ , data , err := conn .Read (c .ctx )
52+ // 设置读取超时(心跳间隔的 3 倍,确保有足够时间接收消息)
53+ readCtx , readCancel := context .WithTimeout (c .ctx , heartbeatInterval * 3 )
54+ _ , data , err := conn .Read (readCtx )
55+ readCancel ()
56+
5357 if err != nil {
5458 if errors .Is (err , context .Canceled ) {
5559 return nil
Original file line number Diff line number Diff line change @@ -107,6 +107,13 @@ func (c *WSClient) sendHeartbeat(ctx context.Context) {
107107 }
108108
109109 if err := c .sendNotifyRequest (req ); err != nil {
110+ logger .Warn ("发送心跳失败,主动关闭连接以触发重连" , "error" , err )
111+ // 主动关闭连接,触发重连机制
112+ c .connMu .Lock ()
113+ if c .conn != nil {
114+ c .conn .Close (websocket .StatusAbnormalClosure , "heartbeat failed" )
115+ }
116+ c .connMu .Unlock ()
110117 return
111118 }
112119 }
You can’t perform that action at this time.
0 commit comments