From 6002f53cbcc6b3af71263515f49f5f086f2c4433 Mon Sep 17 00:00:00 2001 From: moyin <244344649@qq.com> Date: Mon, 5 Jan 2026 11:13:43 +0800 Subject: [PATCH] =?UTF-8?q?config=EF=BC=9A=E4=BC=98=E5=8C=96WebSocket?= =?UTF-8?q?=E8=BF=9C=E7=A8=8B=E8=BF=9E=E6=8E=A5=E7=9A=84CORS=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 明确指定允许的域名列表,包括生产环境域名 - 添加Vite开发服务器端口支持 - 完善CORS方法和头部配置,确保WebSocket握手正常 - 支持xinghangee.icu子域名的通配符匹配 修复远程域名WebSocket连接问题的核心配置 --- src/main.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 03bbfe6..e12ee89 100644 --- a/src/main.ts +++ b/src/main.ts @@ -40,10 +40,17 @@ async function bootstrap() { logger: ['error', 'warn', 'log'], }); - // 允许前端后台(如Vite/React)跨域访问 + // 允许前端后台(如Vite/React)跨域访问,包括WebSocket app.enableCors({ - origin: true, + origin: [ + 'http://localhost:3000', + 'http://localhost:5173', // Vite默认端口 + 'https://whaletownend.xinghangee.icu', + /^https:\/\/.*\.xinghangee\.icu$/ + ], credentials: true, + methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], + allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'], }); // 全局启用校验管道(核心配置)