This commit is contained in:
laodaming 2023-08-30 11:25:20 +08:00
parent f04330c85d
commit 89e714d7d8
2 changed files with 14 additions and 10 deletions

View File

@ -79,6 +79,7 @@ var (
// 每个连接的连接基本属性
type wsConnectItem struct {
conn *websocket.Conn //websocket的连接(基本属性)
userAgent string //用户代理头信息(基本属性,用于重连标识验证因素之一)
logic *DataTransferLogic //logic(基本属性,用于获取上下文,配置或者操作数据库)
closeChan chan struct{} //ws连接关闭chan(基本属性)
isClose bool //是否已经关闭(基本属性)
@ -127,7 +128,7 @@ func (l *DataTransferLogic) DataTransfer(w http.ResponseWriter, r *http.Request)
return
}
//设置连接
ws, err := l.setConnPool(conn, userInfo, isFirefoxBrowser)
ws, err := l.setConnPool(conn, userInfo, isFirefoxBrowser, userAgent)
if err != nil {
conn.Close()
return
@ -147,9 +148,9 @@ func (l *DataTransferLogic) DataTransfer(w http.ResponseWriter, r *http.Request)
}
// 设置连接
func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.UserInfo, isFirefoxBrowser bool) (wsConnectItem, error) {
func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.UserInfo, isFirefoxBrowser bool, userAgent string) (wsConnectItem, error) {
//生成连接唯一标识失败重试10次
uniqueId, err := l.getUniqueId(userInfo, 10)
uniqueId, err := l.getUniqueId(userInfo, userAgent, 10)
if err != nil {
//发送获取唯一标识失败的消息
l.sendGetUniqueIdErrResponse(conn)
@ -157,11 +158,14 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
}
ws := wsConnectItem{
conn: conn,
userAgent: userAgent,
logic: l,
uniqueId: uniqueId,
closeChan: make(chan struct{}, 1),
isClose: false,
uniqueId: uniqueId,
inChan: make(chan []byte, websocketInChanLen),
outChan: make(chan []byte, websocketOutChanLen),
mutex: sync.Mutex{},
userId: userInfo.UserId,
guestId: userInfo.GuestId,
extendRenderProperty: extendRenderProperty{
@ -180,15 +184,15 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
}
// 获取唯一id
func (l *DataTransferLogic) getUniqueId(userInfo *auth.UserInfo, retryTimes int) (uniqueId string, err error) {
func (l *DataTransferLogic) getUniqueId(userInfo *auth.UserInfo, userAgent string, retryTimes int) (uniqueId string, err error) {
if retryTimes < 0 {
return "", errors.New("failed to get unique id")
}
//后面拼接上用户id
uniqueId = hex.EncodeToString([]byte(uuid.New().String())) + getUserJoinPart(userInfo.UserId, userInfo.GuestId)
uniqueId = hex.EncodeToString([]byte(uuid.New().String())) + getUserJoinPart(userInfo.UserId, userInfo.GuestId, userAgent)
//存在则从新获取
if _, ok := mapConnPool.Load(uniqueId); ok {
uniqueId, err = l.getUniqueId(userInfo, retryTimes-1)
uniqueId, err = l.getUniqueId(userInfo, userAgent, retryTimes-1)
if err != nil {
return "", err
}

View File

@ -26,7 +26,7 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) {
}
lendecryptionWid := len(decryptionWid)
//合成client后缀,不是同个后缀的不能复用
userPart := getUserJoinPart(w.userId, w.guestId)
userPart := getUserJoinPart(w.userId, w.guestId, w.userAgent)
lenUserPart := len(userPart)
if lendecryptionWid <= lenUserPart {
w.reuseLastConnErrResponse("length of client id is to short")
@ -62,6 +62,6 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) {
}
// 获取用户拼接部分(复用标识用到)
func getUserJoinPart(userId, guestId int64) string {
return fmt.Sprintf("|_%d_%d_|", userId, guestId)
func getUserJoinPart(userId, guestId int64, userAgent string) string {
return fmt.Sprintf("|_%d_%d_|_%s_|", userId, guestId, userAgent)
}