2023-08-15 10:14:44 +00:00
|
|
|
package logic
|
|
|
|
|
2023-08-23 07:08:45 +00:00
|
|
|
//复用websocket连接标识
|
2023-08-15 10:14:44 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-08-18 09:33:42 +00:00
|
|
|
"fmt"
|
2023-08-15 10:14:44 +00:00
|
|
|
"fusenapi/constants"
|
2023-08-23 08:45:01 +00:00
|
|
|
"fusenapi/utils/encryption_decryption"
|
2023-08-15 10:14:44 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 刷新重连请求恢复上次连接的标识
|
|
|
|
func (w *wsConnectItem) reuseLastConnect(data []byte) {
|
|
|
|
logx.Info("收到请求恢复上次连接标识数据:", string(data))
|
2023-08-24 09:23:41 +00:00
|
|
|
var wid string
|
|
|
|
if err := json.Unmarshal(data, &wid); err != nil {
|
|
|
|
logx.Error(" invalid format of wid :", wid)
|
|
|
|
w.incomeDataFormatErrResponse("invalid format of wid")
|
2023-08-15 10:14:44 +00:00
|
|
|
return
|
|
|
|
}
|
2023-08-23 08:45:01 +00:00
|
|
|
//解密
|
2023-08-24 09:23:41 +00:00
|
|
|
decryptionWid, err := encryption_decryption.CBCDecrypt(wid)
|
2023-08-23 08:45:01 +00:00
|
|
|
if err != nil {
|
2023-08-24 09:23:41 +00:00
|
|
|
w.reuseLastConnErrResponse("invalid wid")
|
2023-08-15 10:14:44 +00:00
|
|
|
return
|
|
|
|
}
|
2023-08-24 09:23:41 +00:00
|
|
|
lendecryptionWid := len(decryptionWid)
|
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)
|
2023-08-24 09:23:41 +00:00
|
|
|
if lendecryptionWid <= lenUserPart {
|
|
|
|
w.reuseLastConnErrResponse("length of client id is to short")
|
2023-08-15 10:38:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
//尾部不同不能复用
|
2023-08-24 09:23:41 +00:00
|
|
|
if decryptionWid[lendecryptionWid-lenUserPart:] != userPart {
|
|
|
|
w.reuseLastConnErrResponse("the client id is not belong you before")
|
2023-08-15 10:38:33 +00:00
|
|
|
return
|
|
|
|
}
|
2023-08-15 10:14:44 +00:00
|
|
|
//存在是不能给他申请重新绑定
|
2023-08-24 09:23:41 +00:00
|
|
|
if v, ok := mapConnPool.Load(wid); ok {
|
2023-08-18 03:05:18 +00:00
|
|
|
obj, ok := v.(wsConnectItem)
|
|
|
|
if !ok {
|
|
|
|
logx.Error("连接断言失败")
|
|
|
|
}
|
2023-08-24 06:11:17 +00:00
|
|
|
//是当前自己占用(无需处理)
|
2023-08-18 03:05:18 +00:00
|
|
|
if obj.uniqueId == w.uniqueId {
|
2023-08-28 02:22:51 +00:00
|
|
|
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
|
|
|
w.sendToOutChan(rsp)
|
2023-08-18 03:05:18 +00:00
|
|
|
return
|
|
|
|
} else {
|
2023-08-24 09:23:41 +00:00
|
|
|
w.reuseLastConnErrResponse("the wid is used by other people")
|
2023-08-18 03:05:18 +00:00
|
|
|
return
|
|
|
|
}
|
2023-08-15 10:14:44 +00:00
|
|
|
}
|
|
|
|
//重新绑定
|
2023-08-24 09:23:41 +00:00
|
|
|
w.uniqueId = wid
|
2023-08-25 03:28:36 +00:00
|
|
|
mapConnPool.Store(wid, *w)
|
2023-08-24 09:23:41 +00:00
|
|
|
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
2023-08-15 10:14:44 +00:00
|
|
|
w.sendToOutChan(rsp)
|
|
|
|
return
|
|
|
|
}
|
2023-08-18 09:33:42 +00:00
|
|
|
|
|
|
|
// 获取用户拼接部分(复用标识用到)
|
|
|
|
func getUserJoinPart(userId, guestId int64) string {
|
2023-08-23 09:03:04 +00:00
|
|
|
return fmt.Sprintf("|_%d_%d_|", userId, guestId)
|
2023-08-18 09:33:42 +00:00
|
|
|
}
|