68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package logic
|
|
|
|
//复用websocket连接标识
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"fusenapi/constants"
|
|
"fusenapi/utils/encryption_decryption"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
// 刷新重连请求恢复上次连接的标识
|
|
func (w *wsConnectItem) reuseLastConnect(data []byte) {
|
|
logx.Info("收到请求恢复上次连接标识数据:", string(data))
|
|
var wid string
|
|
if err := json.Unmarshal(data, &wid); err != nil {
|
|
logx.Error(" invalid format of wid :", wid)
|
|
w.incomeDataFormatErrResponse("invalid format of wid")
|
|
return
|
|
}
|
|
//解密
|
|
decryptionWid, err := encryption_decryption.CBCDecrypt(wid)
|
|
if err != nil {
|
|
w.reuseLastConnErrResponse("invalid wid")
|
|
return
|
|
}
|
|
lendecryptionWid := len(decryptionWid)
|
|
//合成client后缀,不是同个后缀的不能复用
|
|
userPart := getUserJoinPart(w.userId, w.guestId)
|
|
lenUserPart := len(userPart)
|
|
if lendecryptionWid <= lenUserPart {
|
|
w.reuseLastConnErrResponse("length of client id is to short")
|
|
return
|
|
}
|
|
//尾部不同不能复用
|
|
if decryptionWid[lendecryptionWid-lenUserPart:] != userPart {
|
|
w.reuseLastConnErrResponse("the client id is not belong you before")
|
|
return
|
|
}
|
|
//存在是不能给他申请重新绑定
|
|
if v, ok := mapConnPool.Load(wid); ok {
|
|
obj, ok := v.(wsConnectItem)
|
|
if !ok {
|
|
logx.Error("连接断言失败")
|
|
}
|
|
//是当前自己占用(无需处理)
|
|
if obj.uniqueId == w.uniqueId {
|
|
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
|
w.sendToOutChan(rsp)
|
|
return
|
|
} else {
|
|
w.reuseLastConnErrResponse("the wid is used by other people")
|
|
return
|
|
}
|
|
}
|
|
//重新绑定
|
|
w.uniqueId = wid
|
|
mapConnPool.Store(wid, *w)
|
|
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
|
w.sendToOutChan(rsp)
|
|
return
|
|
}
|
|
|
|
// 获取用户拼接部分(复用标识用到)
|
|
func getUserJoinPart(userId, guestId int64) string {
|
|
return fmt.Sprintf("|_%d_%d_|", userId, guestId)
|
|
}
|