fix: ws连接异常中断问题
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/generate"
|
||||
"be.ems/src/modules/ws/model"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
@@ -30,7 +31,7 @@ func (s *WS) UpgraderWs(w http.ResponseWriter, r *http.Request) *websocket.Conn
|
||||
wsUpgrader := websocket.Upgrader{
|
||||
Subprotocols: []string{"omc-ws"},
|
||||
// 设置消息发送缓冲区大小(byte),如果这个值设置得太小,可能会导致服务端在发送大型消息时遇到问题
|
||||
WriteBufferSize: 1024,
|
||||
WriteBufferSize: 4096,
|
||||
// 消息包启用压缩
|
||||
EnableCompression: true,
|
||||
// ws握手超时时间
|
||||
@@ -195,11 +196,18 @@ func (s *WS) ClientWriteListen(wsClient *model.WSClient) {
|
||||
wsClient.MsgChan <- msgByte
|
||||
// 消息发送监听
|
||||
for msg := range wsClient.MsgChan {
|
||||
// PONG句柄
|
||||
if string(msg) == "ws:pong" {
|
||||
wsClient.LastHeartbeat = time.Now().UnixMilli()
|
||||
wsClient.Conn.WriteMessage(websocket.PongMessage, []byte{})
|
||||
continue
|
||||
}
|
||||
// 关闭句柄
|
||||
if string(msg) == "ws:close" {
|
||||
wsClient.Conn.WriteMessage(websocket.CloseMessage, []byte{})
|
||||
return
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
err := wsClient.Conn.WriteMessage(websocket.TextMessage, msg)
|
||||
if err != nil {
|
||||
@@ -207,6 +215,5 @@ func (s *WS) ClientWriteListen(wsClient *model.WSClient) {
|
||||
s.ClientClose(wsClient.ID)
|
||||
return
|
||||
}
|
||||
wsClient.LastHeartbeat = time.Now().UnixMilli()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,14 @@ func (s *WSReceive) Commont(client *model.WSClient, reqMsg model.WSRequest) {
|
||||
case "close":
|
||||
s.close(client)
|
||||
return
|
||||
case "ping", "PING":
|
||||
resByte, _ := json.Marshal(resp.Ok(map[string]any{
|
||||
"requestId": reqMsg.RequestID,
|
||||
"data": "PONG",
|
||||
}))
|
||||
client.MsgChan <- resByte
|
||||
client.MsgChan <- []byte("ws:pong")
|
||||
return
|
||||
case "ps":
|
||||
resByte, err = processor.GetProcessData(reqMsg.RequestID, reqMsg.Data)
|
||||
case "net":
|
||||
|
||||
Reference in New Issue
Block a user