From 84749f4d14458ca40f2cae2d91dff9706698a7d9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 11 Aug 2023 10:49:29 +0800 Subject: [PATCH] fix --- constants/websocket.go | 6 +++++ .../internal/logic/datatransferlogic.go | 8 +++++- .../internal/logic/ws_resume_last_connect.go | 27 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 server/websocket/internal/logic/ws_resume_last_connect.go diff --git a/constants/websocket.go b/constants/websocket.go index be28eb32..ced0c947 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -8,6 +8,12 @@ const ( WEBSOCKET_UNAUTH = "WEBSOCKET_UNAUTH" //ws连接成功 WEBSOCKET_CONNECT_SUCCESS = "WEBSOCKET_CONNECT_SUCCESS" + //请求恢复为上次连接的标识 + WEBSOCKET_REQUEST_RESUME_LAST_CONNECT = "WEBSOCKET_REQUEST_RESUME_LAST_CONNECT" + //请求恢复为上次连接的标识错误 + WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR = "WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR" + //请求恢复为上次连接的标识成功 + WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_SUCCESS = "WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_SUCCESS" //渲染前数据组装 WEBSOCKET_RENDER_IMAGE_ASSEMBLE = "WEBSOCKET_RENDER_IMAGE_ASSEMBLE" //图片渲染 diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 825ce08a..16f822aa 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -112,6 +112,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp //测试的目前写死 39 var userInfo auth.UserInfo userInfo.UserId = 39 + //设置连接 ws := l.setConnPool(conn, userInfo) defer ws.close() //循环读客户端信息 @@ -126,7 +127,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp ws.heartbeat() } -// 获取唯一id +// 设置连接 func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo auth.UserInfo) wsConnectItem { publicMutex.Lock() defer publicMutex.Unlock() @@ -158,6 +159,8 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo auth.User }() return ws } + +// 获取唯一id func (l *DataTransferLogic) getUniqueId() string { uniqueId := uuid.New().String() + time.Now().Format("20060102150405") if _, ok := mapConnPool.Load(uniqueId); ok { @@ -309,6 +312,9 @@ func (w *wsConnectItem) dealwithReciveData(data []byte) { //图片渲染 case constants.WEBSOCKET_RENDER_IMAGE: w.renderImage(d) + //刷新重连请求恢复上次连接的标识 + case constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT: + w.resumeLateConnect(d) default: } diff --git a/server/websocket/internal/logic/ws_resume_last_connect.go b/server/websocket/internal/logic/ws_resume_last_connect.go new file mode 100644 index 00000000..39f29414 --- /dev/null +++ b/server/websocket/internal/logic/ws_resume_last_connect.go @@ -0,0 +1,27 @@ +package logic + +import "fusenapi/constants" + +// 刷新重连请求恢复上次连接的标识 +func (w *wsConnectItem) resumeLateConnect(data []byte) { + clientId := string(data) + //id长度不对 + if len(clientId) != 50 { + rsp := w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "request id is invalid") + w.sendToOutChan(rsp) + return + } + publicMutex.Lock() + defer publicMutex.Unlock() + //存在是不能给他申请重新绑定 + if _, ok := mapConnPool.Load(clientId); ok { + rsp := w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "id has bound by other connect ") + w.sendToOutChan(rsp) + return + } + //重新绑定 + w.uniqueId = clientId + rsp := w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_SUCCESS, clientId) + w.sendToOutChan(rsp) + return +}