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