zhaowenyu
2025-11-15 1e2f717393e4053a3b8de11cbf67cd98a8c77ec1
ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java
@@ -2,7 +2,10 @@
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.component.websocket.utils.WebSocketUtil;
import com.ard.work.domain.enums.MessageType;
import com.ard.work.domain.message.*;
import com.ard.work.service.control.UavControlService;
@@ -14,6 +17,7 @@
import org.springframework.web.socket.handler.TextWebSocketHandler;
import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
@Component
@@ -34,6 +38,8 @@
    private UavControlManager uavControlManager;
    @Resource
    private UavAllManager uavAllManager;
    @Resource
    private TokenService tokenService;
    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) {
@@ -46,6 +52,9 @@
            BaseMessage<?> baseMessage = JSON.parseObject(payload, BaseMessage.class);
            MessageType messageType = MessageType.fromString(baseMessage.getType());
            switch (messageType) {
                case HEARTBEAT:
                    handleHeartbeatMessage(payload, userId);
                    break;
                case STREAM:
                    handleStreamMessage(payload, userId);
                    break;
@@ -68,6 +77,19 @@
        } catch (Exception e) {
            log.error("Error handling message: {}", message, e);
        }
    }
    /**
     * 处理心跳消息
     * <p>
     * 该方法解析给定的负载数据,以获取无人机流消息,并根据消息类型决定是否开始或停止流
     *
     * @param payload 消息的负载数据,通常为JSON字符串
     * @param userId  接收消息的用户ID
     */
    private void handleHeartbeatMessage(String payload, Long userId) {
        WebSocketUtil.sendMessage(userId, payload);
    }
    /**
@@ -276,7 +298,21 @@
        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);
                }
            }
        });
    }
}