From 0c2be975a3df4d6ff24e26d4429325fa2d895fbc Mon Sep 17 00:00:00 2001 From: Baobhan Sith <80159437+Heavrnl@users.noreply.github.com> Date: Fri, 25 Apr 2025 16:51:11 +0800 Subject: [PATCH] Update useWebSocketConnection.ts --- .../src/composables/useWebSocketConnection.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/frontend/src/composables/useWebSocketConnection.ts b/packages/frontend/src/composables/useWebSocketConnection.ts index 43b87de..72a2d06 100644 --- a/packages/frontend/src/composables/useWebSocketConnection.ts +++ b/packages/frontend/src/composables/useWebSocketConnection.ts @@ -150,10 +150,19 @@ export function createWebSocketConnectionManager(sessionId: string, dbConnection isSftpReady.value = false; // 重置 SFTP 状态 try { - ws.value = new WebSocket(url); + // --- 根据页面协议调整 WebSocket URL --- + let secureUrl = url; + if (window.location.protocol === 'https:') { + secureUrl = url.replace(/^ws:/, 'wss:'); + console.log(`[WebSocket ${instanceSessionId}] HTTPS detected, upgrading WebSocket URL to: ${secureUrl}`); + } else { + console.log(`[WebSocket ${instanceSessionId}] HTTP detected, using WebSocket URL: ${secureUrl}`); + } + // --- 使用调整后的 URL --- + ws.value = new WebSocket(secureUrl); ws.value.onopen = () => { - console.log(`[WebSocket ${instanceSessionId}] 连接已打开。`); + console.log(`[WebSocket ${instanceSessionId}] 连接已打开 (${secureUrl})。`); // 在日志中包含使用的 URL reconnectAttempts = 0; // 连接成功,重置尝试次数 statusMessage.value = getStatusText('wsConnected'); // 状态保持 'connecting' 直到收到 ssh:connected