This commit is contained in:
laodaming 2023-08-11 10:49:29 +08:00
parent b0e534de8c
commit 84749f4d14
3 changed files with 40 additions and 1 deletions

View File

@ -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"
//图片渲染

View File

@ -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:
}

View File

@ -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
}