liusuyi
2025-11-13 9c4b29fdf79de231ebbe15668ad40f6e229f8109
增加websocket连接关闭延迟10秒删除token
4 files modified
42 ■■■■■ changed files
ard-framework/src/main/java/com/ard/framework/web/service/TokenService.java 8 ●●●●● patch | view | raw | blame | history
ard-work/pom.xml 6 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/websocket/WebSocketSessionManager.java 5 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java 23 ●●●●● patch | view | raw | blame | history
ard-framework/src/main/java/com/ard/framework/web/service/TokenService.java
@@ -248,4 +248,12 @@
    {
        return Constants.LOGIN_USERID_KEY + userId;
    }
    public LoginUser getLoginUserByUserId(Long userId) {
        String userIdKey = getUserIdKey(userId);
        String tokenKey = redisCache.getCacheObject(userIdKey);
        if (tokenKey == null) return null;
        return redisCache.getCacheObject(tokenKey);
    }
}
ard-work/pom.xml
@@ -24,7 +24,11 @@
            <groupId>com.ard</groupId>
            <artifactId>ard-common</artifactId>
        </dependency>
        <!--  framework框架核心-->
        <dependency>
            <groupId>com.ard</groupId>
            <artifactId>ard-framework</artifactId>
        </dependency>
        <!-- Forest -->
        <dependency>
            <groupId>com.dtflys.forest</groupId>
ard-work/src/main/java/com/ard/work/component/websocket/WebSocketSessionManager.java
@@ -28,4 +28,9 @@
    public static int getOnlineUserCount() {
        return ONLINE_USER_SESSIONS.size();
    }
    public static boolean containsUser(Long userId) {
        return ONLINE_USER_SESSIONS.containsKey(userId);
    }
}
ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java
@@ -2,6 +2,8 @@
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ard.common.core.domain.model.LoginUser;
import com.ard.framework.web.service.TokenService;
import com.ard.work.component.websocket.WebSocketSessionManager;
import com.ard.work.domain.enums.MessageType;
import com.ard.work.domain.message.*;
@@ -12,8 +14,8 @@
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
@Component
@@ -34,6 +36,8 @@
    private UavControlManager uavControlManager;
    @Resource
    private UavAllManager uavAllManager;
    @Resource
    private TokenService tokenService;
    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) {
@@ -276,7 +280,20 @@
        uavTelemetryManager.removeSubscribe(userId);
        uavSignalManager.removeSubscribe(userId);
        uavControlManager.removeSubscribe(userId);
        // 延迟10秒删除token(若期间没重连)
        CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(10_000); // 延迟10秒
            } catch (InterruptedException ignored) {}
            if (!WebSocketSessionManager.containsUser(userId)) {
                LoginUser loginUser = tokenService.getLoginUserByUserId(userId);
                if (loginUser != null) {
                    tokenService.delLoginUser(loginUser.getToken(), userId);
                    log.info("【cmd】用户 [{}] 超时未重连,已删除 token", userId);
                }
            }
        });
    }
}