From 4a7cf89f294c78d711e581125315b7bd01d2e361 Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 14 Jun 2026 11:55:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=89=8D=E7=AB=AF=20WebSocket=20URL=20?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=88=96?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=8E=A8=E5=AF=BC=EF=BC=8C=E6=B6=88=E9=99=A4?= =?UTF-8?q?=20localhost=20=E7=A1=AC=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/lib/websocket.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/frontend/src/lib/websocket.ts b/frontend/src/lib/websocket.ts index 2fd7a9e..7d8c947 100644 --- a/frontend/src/lib/websocket.ts +++ b/frontend/src/lib/websocket.ts @@ -6,7 +6,10 @@ import type { ClientMessage, ServerMessage, WsMessage } from "../types"; -const WS_URL = "ws://localhost:8080/ws"; +// WebSocket 地址:优先使用环境变量,否则基于当前页面地址自动推导 +const WS_URL = + import.meta.env.VITE_WS_URL || + `${window.location.protocol === "https:" ? "wss:" : "ws:"}//${window.location.host}/ws`; const PING_INTERVAL = 30_000; // 30 秒心跳 const MAX_RECONNECT_DELAY = 30_000; // 最大重连延迟 30 秒