diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 8363c5d2..ff929843 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -93,6 +93,7 @@ type userConnPoolCtlChanItem struct { // 每个连接的连接基本属性 type wsConnectItem struct { conn *websocket.Conn //websocket的连接(基本属性) + connExpireTime int64 //websocket过期时间(跟随连接时候的token过期时间) userAgent string //用户代理头信息(基本属性,用于重连标识验证因素之一) logic *DataTransferLogic //logic(基本属性,用于获取上下文,配置或者操作数据库) closeChan chan struct{} //ws连接关闭chan(基本属性) @@ -213,19 +214,25 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use uniqueId = oldWid } } + //默认过期时间 + connExpireTime := time.Now().UTC().Add(time.Second * time.Duration(l.svcCtx.Config.Auth.AccessExpire)).Unix() + if userInfo.Exp > 0 { + connExpireTime = userInfo.Exp + } renderCtx, renderCtxCancelFunc := context.WithCancel(l.ctx) ws := wsConnectItem{ - conn: conn, - userAgent: userAgent, - logic: l, - 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, + conn: conn, + connExpireTime: connExpireTime, + userAgent: userAgent, + logic: l, + 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{ renderChan: make(chan websocket_data.RenderImageReqMsg, renderChanLen), renderCtx: renderCtx, @@ -327,6 +334,12 @@ func (w *wsConnectItem) heartbeat() { case <-w.closeChan: return case <-tick: + //看看token是否过期了 + if w.connExpireTime > 0 && w.connExpireTime < time.Now().UTC().Unix() { + logx.Info("token过期,关闭连接:", w.uniqueId) + w.close() + return + } //发送心跳信息 if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { logx.Error("发送心跳信息异常,关闭连接:", w.uniqueId, err)