()
| 2455 | // WebSocket日志相关 |
| 2456 | // ===================================================================== |
| 2457 | function connectWebSocket() { |
| 2458 | if (AppState.logWebSocket && AppState.logWebSocket.readyState === WebSocket.OPEN) { |
| 2459 | showStatus('WebSocket已经连接', 'info'); |
| 2460 | return; |
| 2461 | } |
| 2462 | |
| 2463 | try { |
| 2464 | const wsPath = new URL('./logs/stream', window.location.href).href; |
| 2465 | const wsUrl = wsPath.replace(/^http/, 'ws'); |
| 2466 | |
| 2467 | // 添加 token 认证参数 |
| 2468 | const wsUrlWithAuth = `${wsUrl}?token=${encodeURIComponent(AppState.authToken)}`; |
| 2469 | |
| 2470 | document.getElementById('connectionStatusText').textContent = '连接中...'; |
| 2471 | document.getElementById('logConnectionStatus').className = 'status info'; |
| 2472 | |
| 2473 | AppState.logWebSocket = new WebSocket(wsUrlWithAuth); |
| 2474 | |
| 2475 | AppState.logWebSocket.onopen = () => { |
| 2476 | document.getElementById('connectionStatusText').textContent = '已连接'; |
| 2477 | document.getElementById('logConnectionStatus').className = 'status success'; |
| 2478 | showStatus('日志流连接成功', 'success'); |
| 2479 | clearLogsDisplay(); |
| 2480 | }; |
| 2481 | |
| 2482 | AppState.logWebSocket.onmessage = (event) => { |
| 2483 | const logLine = event.data; |
| 2484 | if (logLine.trim()) { |
| 2485 | AppState.allLogs.push(logLine); |
| 2486 | if (AppState.allLogs.length > 1000) { |
| 2487 | AppState.allLogs = AppState.allLogs.slice(-1000); |
| 2488 | } |
| 2489 | filterLogs(); |
| 2490 | if (document.getElementById('autoScroll').checked) { |
| 2491 | const logContainer = document.getElementById('logContainer'); |
| 2492 | logContainer.scrollTop = logContainer.scrollHeight; |
| 2493 | } |
| 2494 | } |
| 2495 | }; |
| 2496 | |
| 2497 | AppState.logWebSocket.onclose = () => { |
| 2498 | document.getElementById('connectionStatusText').textContent = '连接断开'; |
| 2499 | document.getElementById('logConnectionStatus').className = 'status error'; |
| 2500 | showStatus('日志流连接断开', 'info'); |
| 2501 | }; |
| 2502 | |
| 2503 | AppState.logWebSocket.onerror = (error) => { |
| 2504 | document.getElementById('connectionStatusText').textContent = '连接错误'; |
| 2505 | document.getElementById('logConnectionStatus').className = 'status error'; |
| 2506 | showStatus('日志流连接错误: ' + error, 'error'); |
| 2507 | }; |
| 2508 | } catch (error) { |
| 2509 | showStatus('创建WebSocket连接失败: ' + error.message, 'error'); |
| 2510 | document.getElementById('connectionStatusText').textContent = '连接失败'; |
| 2511 | document.getElementById('logConnectionStatus').className = 'status error'; |
| 2512 | } |
| 2513 | } |
| 2514 |
no test coverage detected