fix
This commit is contained in:
parent
f04330c85d
commit
89e714d7d8
|
@ -79,6 +79,7 @@ var (
|
||||||
// 每个连接的连接基本属性
|
// 每个连接的连接基本属性
|
||||||
type wsConnectItem struct {
|
type wsConnectItem struct {
|
||||||
conn *websocket.Conn //websocket的连接(基本属性)
|
conn *websocket.Conn //websocket的连接(基本属性)
|
||||||
|
userAgent string //用户代理头信息(基本属性,用于重连标识验证因素之一)
|
||||||
logic *DataTransferLogic //logic(基本属性,用于获取上下文,配置或者操作数据库)
|
logic *DataTransferLogic //logic(基本属性,用于获取上下文,配置或者操作数据库)
|
||||||
closeChan chan struct{} //ws连接关闭chan(基本属性)
|
closeChan chan struct{} //ws连接关闭chan(基本属性)
|
||||||
isClose bool //是否已经关闭(基本属性)
|
isClose bool //是否已经关闭(基本属性)
|
||||||
|
@ -127,7 +128,7 @@ func (l *DataTransferLogic) DataTransfer(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//设置连接
|
//设置连接
|
||||||
ws, err := l.setConnPool(conn, userInfo, isFirefoxBrowser)
|
ws, err := l.setConnPool(conn, userInfo, isFirefoxBrowser, userAgent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return
|
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次)
|
//生成连接唯一标识(失败重试10次)
|
||||||
uniqueId, err := l.getUniqueId(userInfo, 10)
|
uniqueId, err := l.getUniqueId(userInfo, userAgent, 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//发送获取唯一标识失败的消息
|
//发送获取唯一标识失败的消息
|
||||||
l.sendGetUniqueIdErrResponse(conn)
|
l.sendGetUniqueIdErrResponse(conn)
|
||||||
|
@ -157,11 +158,14 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
|
||||||
}
|
}
|
||||||
ws := wsConnectItem{
|
ws := wsConnectItem{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
userAgent: userAgent,
|
||||||
logic: l,
|
logic: l,
|
||||||
uniqueId: uniqueId,
|
|
||||||
closeChan: make(chan struct{}, 1),
|
closeChan: make(chan struct{}, 1),
|
||||||
|
isClose: false,
|
||||||
|
uniqueId: uniqueId,
|
||||||
inChan: make(chan []byte, websocketInChanLen),
|
inChan: make(chan []byte, websocketInChanLen),
|
||||||
outChan: make(chan []byte, websocketOutChanLen),
|
outChan: make(chan []byte, websocketOutChanLen),
|
||||||
|
mutex: sync.Mutex{},
|
||||||
userId: userInfo.UserId,
|
userId: userInfo.UserId,
|
||||||
guestId: userInfo.GuestId,
|
guestId: userInfo.GuestId,
|
||||||
extendRenderProperty: extendRenderProperty{
|
extendRenderProperty: extendRenderProperty{
|
||||||
|
@ -180,15 +184,15 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取唯一id
|
// 获取唯一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 {
|
if retryTimes < 0 {
|
||||||
return "", errors.New("failed to get unique id")
|
return "", errors.New("failed to get unique id")
|
||||||
}
|
}
|
||||||
//后面拼接上用户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 {
|
if _, ok := mapConnPool.Load(uniqueId); ok {
|
||||||
uniqueId, err = l.getUniqueId(userInfo, retryTimes-1)
|
uniqueId, err = l.getUniqueId(userInfo, userAgent, retryTimes-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) {
|
||||||
}
|
}
|
||||||
lendecryptionWid := len(decryptionWid)
|
lendecryptionWid := len(decryptionWid)
|
||||||
//合成client后缀,不是同个后缀的不能复用
|
//合成client后缀,不是同个后缀的不能复用
|
||||||
userPart := getUserJoinPart(w.userId, w.guestId)
|
userPart := getUserJoinPart(w.userId, w.guestId, w.userAgent)
|
||||||
lenUserPart := len(userPart)
|
lenUserPart := len(userPart)
|
||||||
if lendecryptionWid <= lenUserPart {
|
if lendecryptionWid <= lenUserPart {
|
||||||
w.reuseLastConnErrResponse("length of client id is to short")
|
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 {
|
func getUserJoinPart(userId, guestId int64, userAgent string) string {
|
||||||
return fmt.Sprintf("|_%d_%d_|", userId, guestId)
|
return fmt.Sprintf("|_%d_%d_|_%s_|", userId, guestId, userAgent)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user