2023-08-15 10:14:44 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-08-18 09:33:42 +00:00
|
|
|
"fmt"
|
2023-08-15 10:14:44 +00:00
|
|
|
"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
|
|
|
|
}
|
2023-08-15 10:38:33 +00:00
|
|
|
lenClientId := len(clientId)
|
2023-08-15 10:14:44 +00:00
|
|
|
//id长度不对
|
2023-08-15 10:38:33 +00:00
|
|
|
if lenClientId > 100 {
|
2023-08-15 10:14:44 +00:00
|
|
|
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to long"))
|
|
|
|
return
|
|
|
|
}
|
2023-08-15 10:38:33 +00:00
|
|
|
//合成client后缀,不是同个后缀的不能复用
|
2023-08-18 09:33:42 +00:00
|
|
|
userPart := getUserJoinPart(w.userId, w.guestId)
|
2023-08-15 10:38:33 +00:00
|
|
|
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
|
|
|
|
}
|
2023-08-15 10:14:44 +00:00
|
|
|
publicMutex.Lock()
|
|
|
|
defer publicMutex.Unlock()
|
|
|
|
//存在是不能给他申请重新绑定
|
2023-08-18 03:05:18 +00:00
|
|
|
if v, ok := mapConnPool.Load(clientId); ok {
|
|
|
|
obj, ok := v.(wsConnectItem)
|
|
|
|
if !ok {
|
|
|
|
logx.Error("连接断言失败")
|
|
|
|
}
|
|
|
|
//是当前自己占用
|
|
|
|
if obj.uniqueId == w.uniqueId {
|
|
|
|
//重新绑定
|
|
|
|
w.uniqueId = clientId
|
|
|
|
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, clientId)
|
|
|
|
w.sendToOutChan(rsp)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
rsp := w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "id has bound by other connect ")
|
|
|
|
w.sendToOutChan(rsp)
|
|
|
|
return
|
|
|
|
}
|
2023-08-15 10:14:44 +00:00
|
|
|
}
|
|
|
|
//重新绑定
|
|
|
|
w.uniqueId = clientId
|
|
|
|
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, clientId)
|
|
|
|
w.sendToOutChan(rsp)
|
|
|
|
return
|
|
|
|
}
|
2023-08-18 09:33:42 +00:00
|
|
|
|
|
|
|
// 获取用户拼接部分(复用标识用到)
|
|
|
|
func getUserJoinPart(userId, guestId int64) string {
|
|
|
|
return fmt.Sprintf("_%d_%d", userId, guestId)
|
|
|
|
}
|