@@ -104,14 +104,28 @@ type wsConnectItem struct {
// 请求建立连接, 升级websocket协议
func ( l * DataTransferLogic ) DataTransfer ( req * types . DataTransferReq , w http . ResponseWriter , r * http . Request ) {
//把子协议携带的token设置到标准token头信息中
token := r . Header . Get ( "Sec-Websocket-Protocol" )
tokens := r . Header . Get ( "Sec-Websocket-Protocol" )
oldWid := req . Wid
oldWid = strings . Trim ( oldWid , " " )
//有token是正常用户, 无则是白板用户, 也可以连接
if token != "" {
if tokens != "" {
token := ""
debugToken := ""
tokenSlice := strings . Split ( tokens , "," )
switch len ( tokenSlice ) {
case 1 :
token = strings . Trim ( tokenSlice [ 0 ] , " " )
case 2 :
token = strings . Trim ( tokenSlice [ 0 ] , " " )
debugToken = strings . Trim ( tokenSlice [ 1 ] , " " )
r . Header . Set ( "Debug-Token" , debugToken )
default :
logx . Error ( "invalid ws token:" , tokens )
return
}
r . Header . Set ( "Authorization" , "Bearer " + token )
//设置Sec-Websocket-Protocol
upgrader . Subprotocols = [ ] string { token }
upgrader . Subprotocols = [ ] string { tokens }
}
//判断下是否火狐浏览器(获取浏览器第一条消息返回有收不到的bug需要延迟1秒)
userAgent := r . Header . Get ( "User-Agent" )
@@ -225,12 +239,6 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
} ,
debug : userInfo . Debug ,
}
//先设置下debug(后面要删掉)
ws . debug = & auth . Debug {
Exp : & userInfo . Exp ,
IsCache : 1 ,
IsAllTemplateTag : 0 ,
}
//保存连接
mapConnPool . Store ( uniqueId , ws )
//非白板用户, 需要为这个用户建立map索引便于通过用户查询
@@ -238,8 +246,8 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
if isFirefoxBrowser {
time . Sleep ( time . Second * 1 ) //兼容下火狐(直接发回去收不到第一条消息:有待研究)
}
ws . sendToOutChan ( ws . respondDataFormat ( constants . WEBSOCKET_CONNECT_SUCCESS , websocket_data . ConnectSuccessMsg { Wid : uniqueId } ) )
//发送累加统计连接书
ws . sendToOutChan ( ws . respondDataFormat ( constants . WEBSOCKET_CONNECT_SUCCESS , websocket_data . ConnectSuccessMsg { Wid : uniqueId , Debug : ws . debug != nil }) )
//发送累加统计连接数
increaseWebsocketConnectCount ( )
return ws , nil
}
@@ -276,6 +284,7 @@ func (l *DataTransferLogic) checkAuth(r *http.Request) (isAuth bool, userInfo *a
// 解析JWT token,并对空用户进行判断
userInfo , err := basic . ParseJwtToken ( r , l . svcCtx )
if err != nil {
logx . Error ( "未授权:" , err . Error ( ) )
return false , nil
}
if userInfo . UserId > 0 {
@@ -329,6 +338,10 @@ func (w *wsConnectItem) heartbeat() {
w . close ( )
return
}
//查看debug的时间是否过期
if w . debug != nil && w . debug . Exp != nil && * w . debug . Exp < time . Now ( ) . UTC ( ) . Unix ( ) {
w . debug = nil
}
//发送心跳信息
if err := w . conn . WriteMessage ( websocket . PongMessage , nil ) ; err != nil {
logx . Error ( "发送心跳信息异常,关闭连接:" , w . uniqueId , err )