Merge pull request 'test' (#12) from test into prod
Reviewed-on: http://220.205.16.27:18081/goflytec/GoFly-platform-services/pulls/12
This commit is contained in:
+16
-6
@@ -33,7 +33,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
* 使用主题订阅模式,所有设备共享一个WebSocket连接
|
||||
* 基于Spring WebSocket实现
|
||||
*
|
||||
* @author lishilin
|
||||
* @author dzc
|
||||
* @date 2025-01-19
|
||||
*/
|
||||
@Slf4j
|
||||
@@ -72,6 +72,11 @@ public class JetLinksWebSocketClient extends TextWebSocketHandler {
|
||||
*/
|
||||
private volatile WebSocketSession session;
|
||||
|
||||
/**
|
||||
* 发送消息锁(WebSocket不是线程安全的,需要同步发送)
|
||||
*/
|
||||
private final Object sendLock = new Object();
|
||||
|
||||
/**
|
||||
* 重连次数
|
||||
*/
|
||||
@@ -668,14 +673,19 @@ public class JetLinksWebSocketClient extends TextWebSocketHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* 发送消息(线程安全)
|
||||
* WebSocket session 不是线程安全的,必须同步发送以避免 TEXT_PARTIAL_WRITING 错误
|
||||
*/
|
||||
public void send(String message) {
|
||||
if (session != null && session.isOpen()) {
|
||||
try {
|
||||
session.sendMessage(new TextMessage(message));
|
||||
} catch (Exception e) {
|
||||
log.error("发送消息失败: {}", e.getMessage(), e);
|
||||
synchronized (sendLock) {
|
||||
try {
|
||||
if (session != null && session.isOpen()) {
|
||||
session.sendMessage(new TextMessage(message));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("发送消息失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.warn("WebSocket未连接,无法发送消息");
|
||||
|
||||
+24
@@ -265,6 +265,30 @@ public class SubscriptionPoolService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有订阅(服务重启时调用)
|
||||
* 删除所有以 device:sub: 开头的 Redis key
|
||||
*
|
||||
* @return 清理的 key 数量
|
||||
*/
|
||||
public int clearAllSubscriptions() {
|
||||
int deletedCount = 0;
|
||||
try {
|
||||
// 查找所有订阅相关的 key
|
||||
Set<String> keys = redisTemplate.keys(KEY_PREFIX + "*");
|
||||
if (keys != null && !keys.isEmpty()) {
|
||||
deletedCount = keys.size();
|
||||
redisTemplate.delete(keys);
|
||||
log.info("清理所有订阅: 删除了 {} 个 Redis key", deletedCount);
|
||||
} else {
|
||||
log.info("清理所有订阅: 没有找到订阅记录");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("清理所有订阅失败: {}", e.getMessage(), e);
|
||||
}
|
||||
return deletedCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印当前订阅状态(调试用)
|
||||
*/
|
||||
|
||||
+6
-5
@@ -298,9 +298,10 @@
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 12px;
|
||||
color: @text-secondary;
|
||||
color: #00f0ff;
|
||||
padding: 4px 10px;
|
||||
background: rgba(0, 0, 0, 0.4);
|
||||
background: rgba(0, 20, 40, 0.85);
|
||||
border: 1px solid rgba(0, 240, 255, 0.3);
|
||||
border-radius: 12px;
|
||||
user-select: none;
|
||||
|
||||
@@ -313,7 +314,7 @@
|
||||
|
||||
.value {
|
||||
font-weight: 600;
|
||||
color: @text-primary;
|
||||
color: #00ffcc;
|
||||
font-family: 'Consolas', monospace;
|
||||
}
|
||||
|
||||
@@ -328,11 +329,11 @@
|
||||
}
|
||||
|
||||
&.good {
|
||||
.value { color: @accent; }
|
||||
.value { color: #00ff88; }
|
||||
}
|
||||
|
||||
&.warn {
|
||||
.value { color: @warning; }
|
||||
.value { color: #ffaa00; }
|
||||
}
|
||||
|
||||
.fix {
|
||||
|
||||
+14
-8
@@ -153,23 +153,29 @@
|
||||
margin-bottom: 0;
|
||||
padding: 0 24px;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
|
||||
|
||||
|
||||
.ant-tabs-tab {
|
||||
color: rgba(255, 255, 255, 0.4);
|
||||
padding: 16px 0;
|
||||
margin-right: 24px;
|
||||
transition: all 0.3s;
|
||||
&:hover { color: @cyan; }
|
||||
|
||||
.ant-tabs-tab-btn {
|
||||
color: #ffffff !important;
|
||||
font-weight: 600 !important;
|
||||
font-size: 14px !important;
|
||||
}
|
||||
|
||||
&:hover .ant-tabs-tab-btn {
|
||||
color: #ffdd00 !important;
|
||||
}
|
||||
}
|
||||
.ant-tabs-tab-active .ant-tabs-tab-btn {
|
||||
color: @cyan;
|
||||
font-weight: 600;
|
||||
text-shadow: none !important;
|
||||
color: #ffdd00 !important;
|
||||
font-weight: 700 !important;
|
||||
}
|
||||
.ant-tabs-ink-bar {
|
||||
background: @cyan;
|
||||
background: #ffdd00;
|
||||
height: 3px;
|
||||
box-shadow: 0 0 10px @cyan;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+25
-17
@@ -109,24 +109,30 @@
|
||||
.ant-tabs-nav {
|
||||
margin: 0;
|
||||
padding: 0 12px;
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
background: rgba(0, 20, 40, 0.5);
|
||||
|
||||
.ant-tabs-tab {
|
||||
padding: 8px 12px;
|
||||
color: rgba(255, 255, 255, 0.6);
|
||||
font-size: 13px;
|
||||
|
||||
&:hover {
|
||||
color: @cyan;
|
||||
.ant-tabs-tab-btn {
|
||||
color: #ffffff !important;
|
||||
font-size: 14px !important;
|
||||
font-weight: 600 !important;
|
||||
}
|
||||
|
||||
&:hover .ant-tabs-tab-btn {
|
||||
color: #ffdd00 !important;
|
||||
}
|
||||
|
||||
&.ant-tabs-tab-active .ant-tabs-tab-btn {
|
||||
color: @cyan;
|
||||
color: #ffdd00 !important;
|
||||
font-weight: 700 !important;
|
||||
}
|
||||
}
|
||||
|
||||
.ant-tabs-ink-bar {
|
||||
background: @cyan;
|
||||
background: #ffdd00;
|
||||
height: 3px;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +187,7 @@
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 8px;
|
||||
color: rgba(255, 255, 255, 0.7);
|
||||
color: #00f0ff;
|
||||
padding-left: 8px;
|
||||
border-left: 2px solid @cyan;
|
||||
}
|
||||
@@ -190,9 +196,10 @@
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 8px;
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
background: rgba(0, 20, 40, 0.4);
|
||||
border-radius: 6px;
|
||||
padding: 10px;
|
||||
border: 1px solid rgba(0, 240, 255, 0.15);
|
||||
|
||||
.infoItem {
|
||||
display: flex;
|
||||
@@ -202,14 +209,14 @@
|
||||
|
||||
.label {
|
||||
font-size: 10px;
|
||||
color: rgba(255, 255, 255, 0.4);
|
||||
color: #88ccff;
|
||||
}
|
||||
|
||||
.value {
|
||||
font-size: 13px;
|
||||
color: @cyan;
|
||||
color: #00ffcc;
|
||||
font-family: 'Consolas', monospace;
|
||||
font-weight: 500;
|
||||
font-weight: 600;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -298,15 +305,16 @@
|
||||
|
||||
.taskCode {
|
||||
font-size: 11px;
|
||||
color: @cyan;
|
||||
color: #00ffcc;
|
||||
font-family: 'Consolas', monospace;
|
||||
font-weight: 600;
|
||||
}
|
||||
}
|
||||
|
||||
.taskName {
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: #fff;
|
||||
font-weight: 600;
|
||||
color: #00f0ff;
|
||||
margin-bottom: 3px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
@@ -318,11 +326,11 @@
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
font-size: 10px;
|
||||
color: rgba(255, 255, 255, 0.5);
|
||||
color: #aaddff;
|
||||
|
||||
.taskTime {
|
||||
font-size: 10px;
|
||||
color: rgba(255, 255, 255, 0.4);
|
||||
color: #88ccff;
|
||||
}
|
||||
|
||||
.status {
|
||||
|
||||
+77
-3
@@ -21,6 +21,7 @@ import {
|
||||
VideoCameraOutlined,
|
||||
LeftOutlined,
|
||||
RightOutlined,
|
||||
DragOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { DroneData } from '../hooks/useDroneSSE';
|
||||
import { waylineRouteApi, WaylineRouteListItemVO, WaypointVO } from '@/services/waylineRoute';
|
||||
@@ -123,6 +124,13 @@ const TaskPanel: React.FC<TaskPanelProps> = ({
|
||||
const [currentMediaIndex, setCurrentMediaIndex] = useState(0);
|
||||
const [mediaLoading, setMediaLoading] = useState(false);
|
||||
|
||||
// 媒体弹窗拖拽状态
|
||||
const [modalPosition, setModalPosition] = useState({ x: 0, y: 0 });
|
||||
const modalDraggingRef = useRef(false);
|
||||
const modalLastPosRef = useRef({ x: 0, y: 0 });
|
||||
const modalRafRef = useRef<number | null>(null);
|
||||
const modalPendingPosRef = useRef({ x: 0, y: 0 });
|
||||
|
||||
// Tab 状态
|
||||
const [activeTab, setActiveTab] = useState<string>('route');
|
||||
|
||||
@@ -499,6 +507,47 @@ const TaskPanel: React.FC<TaskPanelProps> = ({
|
||||
setCurrentMediaIndex(prev => (prev < previewMediaList.length - 1 ? prev + 1 : 0));
|
||||
}, [previewMediaList.length]);
|
||||
|
||||
// 媒体弹窗拖拽处理
|
||||
const handleModalMouseDown = useCallback((e: React.MouseEvent) => {
|
||||
e.preventDefault();
|
||||
modalDraggingRef.current = true;
|
||||
modalLastPosRef.current = { x: e.clientX, y: e.clientY };
|
||||
modalPendingPosRef.current = { ...modalPosition };
|
||||
document.body.style.userSelect = 'none';
|
||||
document.body.style.cursor = 'grabbing';
|
||||
|
||||
const handleModalMouseMove = (e: MouseEvent) => {
|
||||
if (!modalDraggingRef.current) return;
|
||||
const dx = e.clientX - modalLastPosRef.current.x;
|
||||
const dy = e.clientY - modalLastPosRef.current.y;
|
||||
modalLastPosRef.current = { x: e.clientX, y: e.clientY };
|
||||
modalPendingPosRef.current = {
|
||||
x: modalPendingPosRef.current.x + dx,
|
||||
y: modalPendingPosRef.current.y + dy
|
||||
};
|
||||
if (modalRafRef.current) cancelAnimationFrame(modalRafRef.current);
|
||||
modalRafRef.current = requestAnimationFrame(() => {
|
||||
setModalPosition({ ...modalPendingPosRef.current });
|
||||
});
|
||||
};
|
||||
|
||||
const handleModalMouseUp = () => {
|
||||
modalDraggingRef.current = false;
|
||||
document.body.style.userSelect = '';
|
||||
document.body.style.cursor = '';
|
||||
document.removeEventListener('mousemove', handleModalMouseMove);
|
||||
document.removeEventListener('mouseup', handleModalMouseUp);
|
||||
};
|
||||
|
||||
document.addEventListener('mousemove', handleModalMouseMove);
|
||||
document.addEventListener('mouseup', handleModalMouseUp);
|
||||
}, [modalPosition]);
|
||||
|
||||
// 重置弹窗位置
|
||||
const resetModalPosition = useCallback(() => {
|
||||
setModalPosition({ x: 0, y: 0 });
|
||||
}, []);
|
||||
|
||||
// 当前预览的媒体
|
||||
const currentMedia = useMemo(() => {
|
||||
return previewMediaList[currentMediaIndex];
|
||||
@@ -944,10 +993,20 @@ const TaskPanel: React.FC<TaskPanelProps> = ({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 媒体预览 Modal(支持图片和视频) */}
|
||||
{/* 媒体预览 Modal(支持图片和视频,可拖拽移动) */}
|
||||
<Modal
|
||||
title={
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: 8,
|
||||
cursor: 'move',
|
||||
userSelect: 'none',
|
||||
}}
|
||||
onMouseDown={handleModalMouseDown}
|
||||
>
|
||||
<DragOutlined style={{ color: '#999', fontSize: 14 }} />
|
||||
{currentMedia && isVideoFile(currentMedia.fileType) ? (
|
||||
<VideoCameraOutlined />
|
||||
) : (
|
||||
@@ -962,12 +1021,27 @@ const TaskPanel: React.FC<TaskPanelProps> = ({
|
||||
</div>
|
||||
}
|
||||
open={mediaPreviewVisible}
|
||||
onCancel={() => setMediaPreviewVisible(false)}
|
||||
onCancel={() => {
|
||||
setMediaPreviewVisible(false);
|
||||
resetModalPosition();
|
||||
}}
|
||||
footer={null}
|
||||
width={800}
|
||||
centered
|
||||
destroyOnClose
|
||||
mask={false}
|
||||
maskClosable={false}
|
||||
bodyStyle={{ padding: 0, background: '#000', minHeight: 400 }}
|
||||
modalRender={(modal) => (
|
||||
<div
|
||||
style={{
|
||||
transform: `translate(${modalPosition.x}px, ${modalPosition.y}px)`,
|
||||
pointerEvents: 'auto',
|
||||
}}
|
||||
>
|
||||
{modal}
|
||||
</div>
|
||||
)}
|
||||
>
|
||||
{currentMedia && (
|
||||
<div style={{ position: 'relative' }}>
|
||||
|
||||
+68
-1
@@ -1,6 +1,9 @@
|
||||
import { useState, useRef, useEffect, useCallback } from 'react';
|
||||
import { message } from 'antd';
|
||||
|
||||
// 空闲超时时间:30分钟(毫秒)
|
||||
const IDLE_TIMEOUT_MS = 30 * 60 * 1000;
|
||||
|
||||
// GPS信息
|
||||
interface GpsData {
|
||||
latitude: number;
|
||||
@@ -90,6 +93,7 @@ export interface RealtimeData {
|
||||
interface UseDeviceSSEResult {
|
||||
realtimeData: RealtimeData | null;
|
||||
sseConnected: boolean;
|
||||
isIdleDisconnected: boolean; // 是否因空闲而断开
|
||||
connectSSE: (deviceSn: string) => Promise<void>;
|
||||
disconnectSSE: (deviceSn?: string) => Promise<void>;
|
||||
}
|
||||
@@ -97,6 +101,7 @@ interface UseDeviceSSEResult {
|
||||
export function useDeviceSSE(): UseDeviceSSEResult {
|
||||
const [realtimeData, setRealtimeData] = useState<RealtimeData | null>(null);
|
||||
const [sseConnected, setSSEConnected] = useState(false);
|
||||
const [isIdleDisconnected, setIsIdleDisconnected] = useState(false); // 空闲断开状态
|
||||
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
const currentDeviceSnRef = useRef<string>('');
|
||||
@@ -107,6 +112,7 @@ export function useDeviceSSE(): UseDeviceSSEResult {
|
||||
const reconnectAttemptsRef = useRef<number>(0);
|
||||
const maxReconnectAttempts = 10;
|
||||
const shouldReconnectRef = useRef<boolean>(true);
|
||||
const idleTimerRef = useRef<NodeJS.Timeout | null>(null); // 空闲定时器
|
||||
|
||||
// 核心断开逻辑 - 使用 fetch DELETE 请求(正常关闭时调用)
|
||||
const performDisconnect = (deviceSn: string, sessionId: string, token: string | null) => {
|
||||
@@ -176,6 +182,62 @@ export function useDeviceSSE(): UseDeviceSSEResult {
|
||||
console.log('[DeviceSSE] 连接已断开');
|
||||
}, []);
|
||||
|
||||
// 因空闲断开连接
|
||||
const disconnectDueToIdle = useCallback(() => {
|
||||
console.log('[DeviceSSE] 页面空闲超过30分钟,断开SSE连接以节省资源');
|
||||
setIsIdleDisconnected(true);
|
||||
disconnectSSE();
|
||||
message.warning('页面空闲超过30分钟,已断开实时数据连接,请刷新页面重新连接');
|
||||
}, [disconnectSSE]);
|
||||
|
||||
// 重置空闲定时器
|
||||
const resetIdleTimer = useCallback(() => {
|
||||
// 如果已经因空闲断开,不再重置定时器
|
||||
if (isIdleDisconnected) return;
|
||||
|
||||
// 清除旧定时器
|
||||
if (idleTimerRef.current) {
|
||||
clearTimeout(idleTimerRef.current);
|
||||
}
|
||||
|
||||
// 只有在有 SSE 连接时才启动空闲定时器
|
||||
if (currentDeviceSnRef.current) {
|
||||
idleTimerRef.current = setTimeout(() => {
|
||||
disconnectDueToIdle();
|
||||
}, IDLE_TIMEOUT_MS);
|
||||
}
|
||||
}, [isIdleDisconnected, disconnectDueToIdle]);
|
||||
|
||||
// 监听用户活动事件,重置空闲定时器
|
||||
useEffect(() => {
|
||||
const activityEvents = ['mousedown', 'mousemove', 'keydown', 'scroll', 'touchstart', 'click'];
|
||||
|
||||
const handleActivity = () => {
|
||||
resetIdleTimer();
|
||||
};
|
||||
|
||||
// 添加事件监听
|
||||
activityEvents.forEach(event => {
|
||||
window.addEventListener(event, handleActivity, { passive: true });
|
||||
});
|
||||
|
||||
// 初始启动定时器
|
||||
resetIdleTimer();
|
||||
|
||||
return () => {
|
||||
// 移除事件监听
|
||||
activityEvents.forEach(event => {
|
||||
window.removeEventListener(event, handleActivity);
|
||||
});
|
||||
|
||||
// 清除定时器
|
||||
if (idleTimerRef.current) {
|
||||
clearTimeout(idleTimerRef.current);
|
||||
idleTimerRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [resetIdleTimer]);
|
||||
|
||||
// 处理SSE事件
|
||||
const handleSSEEvent = useCallback((eventName: string, data: string) => {
|
||||
try {
|
||||
@@ -337,10 +399,14 @@ export function useDeviceSSE(): UseDeviceSSEResult {
|
||||
currentDeviceSnRef.current = deviceSn;
|
||||
shouldReconnectRef.current = true; // 启用自动重连
|
||||
reconnectAttemptsRef.current = 0;
|
||||
setIsIdleDisconnected(false); // 重置空闲断开状态
|
||||
|
||||
// 执行连接
|
||||
doConnect(deviceSn);
|
||||
}, [disconnectSSE, doConnect]);
|
||||
|
||||
// 启动空闲定时器
|
||||
resetIdleTimer();
|
||||
}, [disconnectSSE, doConnect, resetIdleTimer]);
|
||||
|
||||
// 监听浏览器刷新/关闭事件 (beforeunload) - 使用 sendBeacon
|
||||
useEffect(() => {
|
||||
@@ -382,6 +448,7 @@ export function useDeviceSSE(): UseDeviceSSEResult {
|
||||
return {
|
||||
realtimeData,
|
||||
sseConnected,
|
||||
isIdleDisconnected,
|
||||
connectSSE,
|
||||
disconnectSSE,
|
||||
};
|
||||
|
||||
+68
-1
@@ -1,6 +1,9 @@
|
||||
import { useState, useRef, useEffect, useCallback } from 'react';
|
||||
import { message } from 'antd';
|
||||
|
||||
// 空闲超时时间:30分钟(毫秒)
|
||||
const IDLE_TIMEOUT_MS = 30 * 60 * 1000;
|
||||
|
||||
// 无人机遥测数据接口定义(从useDeviceSSE.ts复制)
|
||||
export interface DroneData {
|
||||
// 飞行姿态
|
||||
@@ -71,6 +74,7 @@ export interface UseDroneSSEReturn {
|
||||
sseConnected: boolean;
|
||||
loading: boolean;
|
||||
hasData: boolean; // 是否已经收到过数据
|
||||
isIdleDisconnected: boolean; // 是否因空闲而断开
|
||||
|
||||
// 方法
|
||||
connectDroneSSE: (deviceSn: string) => Promise<void>;
|
||||
@@ -95,6 +99,7 @@ export const useDroneSSE = (): UseDroneSSEReturn => {
|
||||
const [sseConnected, setSSEConnected] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [hasData, setHasData] = useState(false); // 是否已经收到过数据
|
||||
const [isIdleDisconnected, setIsIdleDisconnected] = useState(false); // 空闲断开状态
|
||||
|
||||
// Refs
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
@@ -107,6 +112,7 @@ export const useDroneSSE = (): UseDroneSSEReturn => {
|
||||
const reconnectAttemptsRef = useRef<number>(0);
|
||||
const maxReconnectAttempts = 10;
|
||||
const shouldReconnectRef = useRef<boolean>(true); // 是否应该自动重连
|
||||
const idleTimerRef = useRef<NodeJS.Timeout | null>(null); // 空闲定时器
|
||||
|
||||
// 核心断开逻辑 - 使用 fetch DELETE 请求(正常关闭时调用)
|
||||
const performDisconnect = (deviceSn: string, sessionId: string, token: string | null) => {
|
||||
@@ -179,6 +185,62 @@ export const useDroneSSE = (): UseDroneSSEReturn => {
|
||||
reconnectAttemptsRef.current = 0;
|
||||
}, []);
|
||||
|
||||
// 因空闲断开连接
|
||||
const disconnectDueToIdle = useCallback(() => {
|
||||
console.log('[DroneSSE] 页面空闲超过30分钟,断开SSE连接以节省资源');
|
||||
setIsIdleDisconnected(true);
|
||||
disconnectDroneSSE();
|
||||
message.warning('页面空闲超过30分钟,已断开无人机数据连接,请刷新页面重新连接');
|
||||
}, [disconnectDroneSSE]);
|
||||
|
||||
// 重置空闲定时器
|
||||
const resetIdleTimer = useCallback(() => {
|
||||
// 如果已经因空闲断开,不再重置定时器
|
||||
if (isIdleDisconnected) return;
|
||||
|
||||
// 清除旧定时器
|
||||
if (idleTimerRef.current) {
|
||||
clearTimeout(idleTimerRef.current);
|
||||
}
|
||||
|
||||
// 只有在有 SSE 连接时才启动空闲定时器
|
||||
if (currentDeviceSnRef.current) {
|
||||
idleTimerRef.current = setTimeout(() => {
|
||||
disconnectDueToIdle();
|
||||
}, IDLE_TIMEOUT_MS);
|
||||
}
|
||||
}, [isIdleDisconnected, disconnectDueToIdle]);
|
||||
|
||||
// 监听用户活动事件,重置空闲定时器
|
||||
useEffect(() => {
|
||||
const activityEvents = ['mousedown', 'mousemove', 'keydown', 'scroll', 'touchstart', 'click'];
|
||||
|
||||
const handleActivity = () => {
|
||||
resetIdleTimer();
|
||||
};
|
||||
|
||||
// 添加事件监听
|
||||
activityEvents.forEach(event => {
|
||||
window.addEventListener(event, handleActivity, { passive: true });
|
||||
});
|
||||
|
||||
// 初始启动定时器
|
||||
resetIdleTimer();
|
||||
|
||||
return () => {
|
||||
// 移除事件监听
|
||||
activityEvents.forEach(event => {
|
||||
window.removeEventListener(event, handleActivity);
|
||||
});
|
||||
|
||||
// 清除定时器
|
||||
if (idleTimerRef.current) {
|
||||
clearTimeout(idleTimerRef.current);
|
||||
idleTimerRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [resetIdleTimer]);
|
||||
|
||||
// 处理SSE事件(专门处理无人机数据)
|
||||
const handleSSEEvent = useCallback((eventName: string, data: string) => {
|
||||
try {
|
||||
@@ -360,10 +422,14 @@ export const useDroneSSE = (): UseDroneSSEReturn => {
|
||||
currentDeviceSnRef.current = deviceSn;
|
||||
shouldReconnectRef.current = true; // 启用自动重连
|
||||
reconnectAttemptsRef.current = 0;
|
||||
setIsIdleDisconnected(false); // 重置空闲断开状态
|
||||
|
||||
// 执行连接
|
||||
doConnect(deviceSn);
|
||||
}, [disconnectDroneSSE, doConnect]);
|
||||
|
||||
// 启动空闲定时器
|
||||
resetIdleTimer();
|
||||
}, [disconnectDroneSSE, doConnect, resetIdleTimer]);
|
||||
|
||||
// 重置无人机数据
|
||||
const resetDroneData = useCallback(() => {
|
||||
@@ -415,6 +481,7 @@ export const useDroneSSE = (): UseDroneSSEReturn => {
|
||||
sseConnected,
|
||||
loading,
|
||||
hasData,
|
||||
isIdleDisconnected,
|
||||
|
||||
// 方法
|
||||
connectDroneSSE,
|
||||
|
||||
Reference in New Issue
Block a user