|
@ -10,17 +10,11 @@ const END_SEQUENCE = '\n\n' // 消息结束标志 |
|
|
const tcpClients = new Map() |
|
|
const tcpClients = new Map() |
|
|
// 保存待处理的请求,用于关联响应
|
|
|
// 保存待处理的请求,用于关联响应
|
|
|
const pendingRequests = new Map() |
|
|
const pendingRequests = new Map() |
|
|
// 重连配置和状态管理
|
|
|
|
|
|
const reconnectConfig = { |
|
|
|
|
|
enabled: false, |
|
|
|
|
|
interval: 5 // 默认5秒
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 创建重连管理器实例
|
|
|
// 创建重连管理器实例
|
|
|
const reconnectManager = new ReconnectManager() |
|
|
const reconnectManager = new ReconnectManager() |
|
|
|
|
|
// 处理重连尝试的回调函数
|
|
|
// 配置重连管理器的事件处理
|
|
|
const handleAttemptReconnect = (ip, callback) => { |
|
|
reconnectManager.on('attempt-reconnect', (ip, callback) => { |
|
|
|
|
|
// 获取重连信息
|
|
|
// 获取重连信息
|
|
|
const connectionData = reconnectManager.getReconnectStatus(ip) |
|
|
const connectionData = reconnectManager.getReconnectStatus(ip) |
|
|
if (!connectionData.config.enabled) { |
|
|
if (!connectionData.config.enabled) { |
|
@ -40,7 +34,7 @@ reconnectManager.on('attempt-reconnect', (ip, callback) => { |
|
|
`Connection timeout set to ${connectionTimeout}ms (reconnect interval: ${reconnectInterval}ms)` |
|
|
`Connection timeout set to ${connectionTimeout}ms (reconnect interval: ${reconnectInterval}ms)` |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// 需要从某处获取端口和eventSender - 这里需要存储这些信息
|
|
|
// 需要从某处获取端口和eventSender
|
|
|
const storedConnectionData = tcpClients.get(ip) || tcpConnectionData.get(ip) |
|
|
const storedConnectionData = tcpClients.get(ip) || tcpConnectionData.get(ip) |
|
|
if (!storedConnectionData) { |
|
|
if (!storedConnectionData) { |
|
|
callback(false, 'Connection data not found') |
|
|
callback(false, 'Connection data not found') |
|
@ -112,10 +106,10 @@ reconnectManager.on('attempt-reconnect', (ip, callback) => { |
|
|
log.debug(`Reconnect attempt connection closed for ${ip}`) |
|
|
log.debug(`Reconnect attempt connection closed for ${ip}`) |
|
|
// 连接关闭由心跳检测处理
|
|
|
// 连接关闭由心跳检测处理
|
|
|
}) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
// 处理重连状态更新
|
|
|
// 处理重连状态更新的回调函数
|
|
|
reconnectManager.on('reconnect-status', (status) => { |
|
|
const handleReconnectStatus = (status) => { |
|
|
// 通知所有渲染进程重连状态变化
|
|
|
// 通知所有渲染进程重连状态变化
|
|
|
const storedConnectionData = tcpConnectionData.get(status.ip) |
|
|
const storedConnectionData = tcpConnectionData.get(status.ip) |
|
|
if (storedConnectionData && storedConnectionData.eventSender) { |
|
|
if (storedConnectionData && storedConnectionData.eventSender) { |
|
@ -147,7 +141,11 @@ reconnectManager.on('reconnect-status', (status) => { |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 配置重连管理器事件监听
|
|
|
|
|
|
reconnectManager.on('attempt-reconnect', handleAttemptReconnect) |
|
|
|
|
|
reconnectManager.on('reconnect-status', handleReconnectStatus) |
|
|
|
|
|
|
|
|
// 存储连接数据以供重连使用
|
|
|
// 存储连接数据以供重连使用
|
|
|
const tcpConnectionData = new Map() // 存储连接参数
|
|
|
const tcpConnectionData = new Map() // 存储连接参数
|
|
@ -339,6 +337,7 @@ const connectDevice = (event, { ip, port }) => { |
|
|
|
|
|
|
|
|
// 只有在非主动断开的情况下才启动重连
|
|
|
// 只有在非主动断开的情况下才启动重连
|
|
|
// 主动断开会先调用 disconnectDevice 函数
|
|
|
// 主动断开会先调用 disconnectDevice 函数
|
|
|
|
|
|
const reconnectConfig = reconnectManager.getConfig() |
|
|
if (reconnectConfig.enabled) { |
|
|
if (reconnectConfig.enabled) { |
|
|
log.info(`Connection lost to ${ip}, starting reconnect...`) |
|
|
log.info(`Connection lost to ${ip}, starting reconnect...`) |
|
|
// 保存连接数据供重连使用
|
|
|
// 保存连接数据供重连使用
|
|
@ -1355,16 +1354,15 @@ const delay = () => { |
|
|
|
|
|
|
|
|
// 处理重连配置
|
|
|
// 处理重连配置
|
|
|
const handleReconnectConfig = (event, { enabled, interval }) => { |
|
|
const handleReconnectConfig = (event, { enabled, interval }) => { |
|
|
reconnectConfig.enabled = enabled |
|
|
const validInterval = Math.max(4, interval) // 最小4秒
|
|
|
reconnectConfig.interval = Math.max(4, interval) // 最小4秒
|
|
|
|
|
|
|
|
|
|
|
|
// 更新重连管理器配置
|
|
|
// 更新重连管理器配置
|
|
|
reconnectManager.updateConfig({ |
|
|
reconnectManager.updateConfig({ |
|
|
enabled, |
|
|
enabled, |
|
|
interval: reconnectConfig.interval |
|
|
interval: validInterval |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
log.info('Reconnect config updated:', { enabled, interval: reconnectConfig.interval }) |
|
|
log.info('Reconnect config updated:', { enabled, interval: validInterval }) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 启动心跳检测
|
|
|
// 启动心跳检测
|
|
@ -1421,6 +1419,7 @@ const checkHeartbeat = (ip) => { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 启动重连(如果启用)
|
|
|
// 启动重连(如果启用)
|
|
|
|
|
|
const reconnectConfig = reconnectManager.getConfig() |
|
|
if (reconnectConfig.enabled) { |
|
|
if (reconnectConfig.enabled) { |
|
|
log.info(`Connection lost to ${ip} due to heartbeat timeout, starting reconnect...`) |
|
|
log.info(`Connection lost to ${ip} due to heartbeat timeout, starting reconnect...`) |
|
|
// 保存连接数据供重连使用
|
|
|
// 保存连接数据供重连使用
|
|
|