|
@@ -86,11 +86,13 @@ func (c *Client) reader() {
|
|
|
//
|
|
|
// 客户端断开重新连接后无法保证首次发送消息,所以按照之前的 firstReadWait 无法保证一直连接
|
|
|
// 这里设置一个写周期,让客户端持续保持,不会因为重连后没有在规定时间内发送心跳而断开
|
|
|
- writeReadDeadlineTime := time.Now().Add(c.writeWait)
|
|
|
- _ = c.UnderlyingConn.SetWriteDeadline(writeReadDeadlineTime)
|
|
|
+ //_ = c.UnderlyingConn.SetReadDeadline(time.Now().Add(c.writeWait))
|
|
|
|
|
|
for {
|
|
|
|
|
|
+ // 刷新超时时间, 客户端让客户端保持
|
|
|
+ _ = c.UnderlyingConn.SetReadDeadline(time.Now().Add(c.heartbeatWait))
|
|
|
+
|
|
|
// 接收消息
|
|
|
msgType, bytes, err := c.UnderlyingConn.ReadMessage()
|
|
|
if err != nil {
|
|
@@ -110,8 +112,7 @@ func (c *Client) reader() {
|
|
|
// 收到客户端 websocket 的 ping 消息
|
|
|
// 收到后默认返回,用途告诉客户端这条连接还是需要保留着
|
|
|
if msgType == websocket.PingMessage {
|
|
|
- writeReadDeadlineTime := time.Now().Add(c.writeWait)
|
|
|
- c.UnderlyingConn.SetWriteDeadline(writeReadDeadlineTime)
|
|
|
+ c.UnderlyingConn.SetWriteDeadline(time.Now().Add(c.writeWait))
|
|
|
_ = c.UnderlyingConn.WriteMessage(websocket.PongMessage, []byte(""))
|
|
|
continue
|
|
|
}
|
|
@@ -120,9 +121,7 @@ func (c *Client) reader() {
|
|
|
message := deserializeMessage(bytes)
|
|
|
zap.L().Info("[reader] 收到消息", zap.String("user_id", c.UserId), zap.Any("message", message))
|
|
|
|
|
|
- // 刷新超时时间, 客户端让客户端保持
|
|
|
- heartbeatReadDeadlineTime := time.Now().Add(c.heartbeatWait)
|
|
|
- _ = c.UnderlyingConn.SetReadDeadline(heartbeatReadDeadlineTime)
|
|
|
+ //_ = c.UnderlyingConn.SetReadDeadline(time.Now().Add(c.heartbeatWait))
|
|
|
|
|
|
switch message.MessageType() {
|
|
|
case MessageTypePingPong:
|