// src/utils/websocket.js let ws = null; let reconnectAttempts = 0; let pingTimer = null; // 用于管理心跳定时器 const maxReconnectAttempts = 5; const reconnectInterval = 5000; // 5秒重连间隔 const pingInterval = 5000; // 5秒发送ping export function initWebSocket(token, onMessage) { if (!token) { console.error('WebSocket 初始化失败:缺少 token'); return; } // 如果 ws 已存在且连接活跃,则跳过初始化 if (ws && ws.readyState === WebSocket.OPEN) { console.log('WebSocket 已连接,跳过重复初始化'); return; } // 如果 ws 存在但未关闭,先关闭旧连接 if (ws) { console.warn('发现旧 WebSocket 连接,正在关闭...'); closeWebSocket(); } // 取当前页面的协议(http/https),主机(域名 + 端口) const loc = window.location; const protocol = loc.protocol === 'https:' ? 'wss:' : 'ws:'; // 安全协议自动切换 const host = loc.host; // 域名 + 端口,比如 localhost:8080 // 拼接 WebSocket 地址 const wsUrl = `${protocol}//${host}/ws?token=${token}`; console.log(wsUrl,"wsUrl"); ws = new WebSocket(wsUrl); ws.onopen = () => { reconnectAttempts = 0; // 重置重连计数 // 启动心跳机制 if (pingTimer) clearInterval(pingTimer); // 清理旧定时器 pingTimer = setInterval(() => { if (ws && ws.readyState === WebSocket.OPEN) { ws.send('ping'); } else { console.warn('WebSocket 未连接,停止 ping'); clearInterval(pingTimer); pingTimer = null; } }, pingInterval); }; ws.onmessage = (event) => { const data = event.data; console.log('WebSocket 收到原始消息:', data); if (data === 'pong') { console.log('收到 pong 响应,连接活跃'); reconnectAttempts = 0; // 重置重连计数,确保活跃连接不触发重连 return; } onMessage('message', data); }; ws.onerror = (error) => { console.error('WebSocket 错误:', error); }; ws.onclose = () => { console.warn('WebSocket 连接关闭,时间:', new Date()); if (pingTimer) { clearInterval(pingTimer); pingTimer = null; } if (reconnectAttempts < maxReconnectAttempts) { reconnectAttempts++; console.log(`尝试重连 (${reconnectAttempts}/${maxReconnectAttempts})...`); setTimeout(() => { initWebSocket(token, onMessage); }, reconnectInterval); } else { console.error('达到最大重连次数,停止重连'); onMessage('error', 'WebSocket 连接失败,已达最大重连次数'); } }; // 清理 WebSocket window.addEventListener('beforeunload', () => { closeWebSocket(); }, { once: true }); // 确保事件监听只添加一次 } export function closeWebSocket() { if (ws) { ws.close(); ws = null; if (pingTimer) { clearInterval(pingTimer); pingTimer = null; } console.log('WebSocket 已关闭'); } }