This commit is contained in:
laodaming 2023-10-12 14:44:09 +08:00
parent e2b174a58f
commit 22ee293bfb

View File

@ -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,9 +214,15 @@ 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,
connExpireTime: connExpireTime,
userAgent: userAgent,
logic: l,
closeChan: make(chan struct{}, 1),
@ -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)