From 2b12e7c64ed509026fb9264c0cb1c523cc9f0af0 Mon Sep 17 00:00:00 2001 From: ihaiyan Date: Wed, 12 May 2021 09:32:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4websocket=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/websocket/core/client.go | 11 +++++++---- config/config.yml | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/utils/websocket/core/client.go b/app/utils/websocket/core/client.go index 628d1ed..89741ad 100644 --- a/app/utils/websocket/core/client.go +++ b/app/utils/websocket/core/client.go @@ -1,14 +1,15 @@ package core import ( - "github.com/gin-gonic/gin" - "github.com/gorilla/websocket" - "go.uber.org/zap" "goskeleton/app/global/my_errors" "goskeleton/app/global/variable" "net/http" "sync" "time" + + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" + "go.uber.org/zap" ) type Client struct { @@ -129,6 +130,8 @@ func (c *Client) Heartbeat() { //2.浏览器收到服务器的ping格式消息,会自动响应pong消息,将服务器消息原路返回过来 if c.ReadDeadline == 0 { _ = c.Conn.SetReadDeadline(time.Time{}) + } else { + _ = c.Conn.SetReadDeadline(time.Now().Add(c.ReadDeadline)) } c.Conn.SetPongHandler(func(receivedPong string) error { if c.ReadDeadline > time.Nanosecond { @@ -152,7 +155,7 @@ func (c *Client) Heartbeat() { } } else { if c.HeartbeatFailTimes > 0 { - c.HeartbeatFailTimes-- + c.HeartbeatFailTimes = 0 } } } else { diff --git a/config/config.yml b/config/config.yml index d40a0a5..289779e 100644 --- a/config/config.yml +++ b/config/config.yml @@ -39,9 +39,9 @@ Websocket: #该服务与Http具有相同的ip、端口,因此不需要额外 Start: 0 #默认不启动该服务(1=启动;0=不启动) WriteReadBufferSize: 20480 # 读写缓冲区分配字节,大概能存储 6800 多一点的文字 MaxMessageSize: 65535 # 从消息管道读取消息的最大字节 - PingPeriod: 30 #心跳包频率,单位:秒 + PingPeriod: 20 #心跳包频率,单位:秒 HeartbeatFailMaxTimes: 4 # 允许心跳失败的最大次数(默认设置为PingPeriod=30秒检测一次,连续4次没有心跳就会清除后端在线信息) - ReadDeadline: 0 # 客户端在线情况下,正常的业务消息间隔秒数必须小于该值,否则服务器将会主动断开,单位:秒。 0 表示不设限制,即服务器不主动断开不发送任何消息的在线客户端 + ReadDeadline: 100 # 客户端在线情况下,正常的业务消息间隔秒数必须小于该值,否则服务器将会主动断开,该值不能小于心跳频率*允许失败次数,单位:秒。 0 表示不设限制,即服务器不主动断开不发送任何消息的在线客户端,但会消耗服务器资源 WriteDeadline: 35 # 消息单次写入超时时间,单位:秒 SnowFlake: -- Gitee