From 68ed14b373b84cfb2de5727c176a6fe221bcf4cb Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 15 Aug 2023 18:38:33 +0800 Subject: [PATCH] fix --- .../internal/logic/datatransferlogic.go | 19 +++++++++++++------ .../internal/logic/ws_reuse_last_connect.go | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 1c1b05dd..777d20b4 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -3,6 +3,7 @@ package logic import ( "bytes" "encoding/json" + "fmt" "fusenapi/constants" "fusenapi/initalize" "fusenapi/model/gmodel" @@ -97,7 +98,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp ) isAuth, userInfo = l.checkAuth(svcCtx, r) if !isAuth { - time.Sleep(time.Second * 2) //兼容下火狐 + time.Sleep(time.Second * 1) //兼容下火狐 rsp := websocket_data.DataTransferData{ T: constants.WEBSOCKET_UNAUTH, D: nil, @@ -132,7 +133,7 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo auth.User publicMutex.Lock() defer publicMutex.Unlock() //生成连接唯一标识 - uniqueId := l.getUniqueId() + uniqueId := l.getUniqueId(userInfo) ws := wsConnectItem{ conn: conn, ctx: l.ctx, @@ -153,21 +154,27 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo auth.User mapConnPool.Store(uniqueId, ws) go func() { //把连接成功消息发回去 - time.Sleep(time.Second * 2) //兼容下火狐 + time.Sleep(time.Second * 1) //兼容下火狐 ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, uniqueId)) }() return ws } // 获取唯一id -func (l *DataTransferLogic) getUniqueId() string { - uniqueId := uuid.New().String() + time.Now().Format("20060102150405") +func (l *DataTransferLogic) getUniqueId(userInfo auth.UserInfo) string { + //后面拼接上用户id + uniqueId := uuid.New().String() + getUserPart(userInfo.UserId, userInfo.GuestId) if _, ok := mapConnPool.Load(uniqueId); ok { - uniqueId = l.getUniqueId() + uniqueId = l.getUniqueId(userInfo) } return uniqueId } +// 获取用户拼接部分 +func getUserPart(userId, guestId int64) string { + return fmt.Sprintf("_%d_%d", userId, guestId) +} + // 鉴权 func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Request) (isAuth bool, userInfo *auth.UserInfo) { // 解析JWT token,并对空用户进行判断 diff --git a/server/websocket/internal/logic/ws_reuse_last_connect.go b/server/websocket/internal/logic/ws_reuse_last_connect.go index 325bb292..ea8c75f1 100644 --- a/server/websocket/internal/logic/ws_reuse_last_connect.go +++ b/server/websocket/internal/logic/ws_reuse_last_connect.go @@ -15,11 +15,24 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) { w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "invalid format of client id")) return } + lenClientId := len(clientId) //id长度不对 - if len(clientId) > 100 { + if lenClientId > 100 { w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to long")) return } + //合成client后缀,不是同个后缀的不能复用 + userPart := getUserPart(w.userId, w.guestId) + lenUserPart := len(userPart) + if lenClientId <= lenUserPart { + w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to short")) + return + } + //尾部不同不能复用 + if clientId[lenClientId-lenUserPart:] != userPart { + w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "the client id is not belong you before")) + return + } publicMutex.Lock() defer publicMutex.Unlock() //存在是不能给他申请重新绑定