diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 0d8571f6..e0ac8a3f 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -81,16 +81,16 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp defer conn.Close() rsp := types.DataTransferData{} //鉴权不成功10秒后断开 - if !l.checkAuth(svcCtx, r) { + /*if !l.checkAuth(svcCtx, r) { rsp.T = constants.WEBSOCKET_UNAUTH rsp.D = "unAuth" b, _ := json.Marshal(rsp) + //先发一条正常信息 _ = conn.WriteMessage(websocket.TextMessage, b) - select { - case <-time.After(time.Second * 10): //10秒后断开 - return - } - } + //发送关闭信息 + _ = conn.WriteMessage(websocket.CloseMessage, nil) + return + }*/ //生成连接唯一标识 flag := uuid.New().String() + time.Now().Format("20060102150405") ws := wsConnectItem{ @@ -147,11 +147,6 @@ func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Reques // 心跳 func (w *wsConnectItem) heartbeat() { - rsp := types.DataTransferData{ - T: constants.WEBSOCKET_HEARTBEAT, - D: "", - } - b, _ := json.Marshal(rsp) for { time.Sleep(time.Second * 10) select { @@ -159,7 +154,7 @@ func (w *wsConnectItem) heartbeat() { return default: //发送心跳信息 - if err := w.conn.WriteMessage(websocket.TextMessage, b); err != nil { + if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) w.close() return @@ -173,6 +168,8 @@ func (w *wsConnectItem) close() { w.mutex.Lock() defer w.mutex.Unlock() logx.Info("websocket:", w.flag, " is closing...") + //发送关闭信息 + _ = w.conn.WriteMessage(websocket.CloseMessage, nil) w.conn.Close() mapConnPool.Delete(w.flag) if !w.isClose {