From c28a5a6de0d286bfa00354a35317ee00f06fd645 Mon Sep 17 00:00:00 2001 From: Baobhan Sith <80159437+Heavrnl@users.noreply.github.com> Date: Thu, 1 May 2025 15:07:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=86=85=E7=BD=91?= =?UTF-8?q?=E8=AE=BF=E9=97=AEws=E8=B7=AF=E5=BE=84=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to #1 --- packages/frontend/package.json | 2 +- packages/frontend/src/stores/session.store.ts | 29 +++++-------------- packages/frontend/vite.config.ts | 7 +++-- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/packages/frontend/package.json b/packages/frontend/package.json index 80d0ba0..87f8161 100644 --- a/packages/frontend/package.json +++ b/packages/frontend/package.json @@ -4,7 +4,7 @@ "private": true, "type": "module", "scripts": { - "dev": "vite", + "dev": "vite --host", "build": "vue-tsc --noEmit && vite build", "preview": "vite preview" }, diff --git a/packages/frontend/src/stores/session.store.ts b/packages/frontend/src/stores/session.store.ts index cb62f97..74063a3 100644 --- a/packages/frontend/src/stores/session.store.ts +++ b/packages/frontend/src/stores/session.store.ts @@ -198,18 +198,10 @@ export const useSessionStore = defineStore('session', () => { // 根据当前页面协议动态生成 WebSocket URL,并移除硬编码的端口 // 假设 WebSocket 服务与 Web 服务在同一主机和端口上提供(通过反向代理) const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - // 移除端口 :3001,依赖反向代理或同源策略 - // 如果 WebSocket 有特定路径 (例如 /ws),需要在这里添加 - // --- 修改:根据环境确定 WebSocket 主机 --- - let wsHost = window.location.hostname; - if (window.location.hostname === 'localhost') { - wsHost = 'localhost:3001'; // 本地调试时硬编码 - console.log('[SessionStore openNewSession] Using hardcoded localhost:3001 for WebSocket connection.'); - } else { - console.log(`[SessionStore openNewSession] Using hostname: ${wsHost}`); - } - // --- 结束修改 --- - const wsUrl = `${protocol}//${wsHost}/ws/`; // 使用 wsHost 并添加 /ws/ 路径 + // 使用 window.location.host,它包含主机名和端口(如果非默认) + const wsHostAndPort = window.location.host; + // 假设 WebSocket 路径固定为 /ws/ + const wsUrl = `${protocol}//${wsHostAndPort}/ws/`; console.log(`[SessionStore] Generated WebSocket URL: ${wsUrl}`); // 添加日志记录生成的 URL wsManager.connect(wsUrl); console.log(`[SessionStore] 已为会话 ${newSessionId} 启动 WebSocket 连接。`); @@ -398,19 +390,14 @@ export const useSessionStore = defineStore('session', () => { // 满足最高优先级:重连当前活动会话 console.log(`[SessionStore] 活动会话 ${activeSessionId.value} 已断开或出错,尝试重连...`); const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - let wsHost = window.location.hostname; - if (window.location.hostname === 'localhost') { - wsHost = 'localhost:3001'; - console.log('[SessionStore handleConnectRequest] Using hardcoded localhost:3001 for WebSocket reconnect.'); - } else { - console.log(`[SessionStore handleConnectRequest] Using hostname: ${wsHost}`); - } - const wsUrl = `${protocol}//${wsHost}/ws/`; + // 使用 window.location.host + const wsHostAndPort = window.location.host; + const wsUrl = `${protocol}//${wsHostAndPort}/ws/`; console.log(`[SessionStore handleConnectRequest] Generated WebSocket URL for reconnect: ${wsUrl}`); currentActiveSession.wsManager.connect(wsUrl); // 重连后,确保激活该会话(如果它不是活动会话)并导航 activateSession(activeSessionId.value); // 确保激活 - router.push({ name: 'Workspace' }); // +++ 添加跳转 +++ + router.push({ name: 'Workspace' }); } } } diff --git a/packages/frontend/vite.config.ts b/packages/frontend/vite.config.ts index b52dd1b..b98e9b9 100644 --- a/packages/frontend/vite.config.ts +++ b/packages/frontend/vite.config.ts @@ -19,14 +19,17 @@ export default defineConfig({ // 可选:如果后端 API 路径没有 /api 前缀,可以在这里重写路径 // rewrite: (path) => path.replace(/^\/api/, '') }, - // --- 新增开始 --- // 将所有 /uploads 开头的请求也代理到后端服务器 '/uploads': { target: 'http://localhost:3001', // 后端服务器地址 changeOrigin: true, // 对于静态资源通常也建议开启 // 通常不需要重写静态资源的路径 + }, + '/ws': { + target: 'ws://localhost:3001', // 后端 WebSocket 服务器地址 + ws: true, + changeOrigin: true, } - // --- 新增结束 --- } } })