fusenapi/server/websocket/internal/logic/ws_reuse_last_connect.go
laodaming d603cacd0b fix
2023-08-15 18:14:44 +08:00

37 lines
1.1 KiB
Go

package logic
import (
"encoding/json"
"fusenapi/constants"
"github.com/zeromicro/go-zero/core/logx"
)
// 刷新重连请求恢复上次连接的标识
func (w *wsConnectItem) reuseLastConnect(data []byte) {
logx.Info("收到请求恢复上次连接标识数据:", string(data))
var clientId string
if err := json.Unmarshal(data, &clientId); err != nil {
logx.Error(" invalid format of client id :", clientId)
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "invalid format of client id"))
return
}
//id长度不对
if len(clientId) > 100 {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to long"))
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_CONNECT_SUCCESS, clientId)
w.sendToOutChan(rsp)
return
}