From 25b786bcad14159e7d78b1ee764d29e7634df3e2 Mon Sep 17 00:00:00 2001 From: chenhang Date: Tue, 23 Jun 2026 18:35:15 +0800 Subject: [PATCH] fix(chat): omit websocket token in development --- src/core/net/chat-websocket.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/net/chat-websocket.ts b/src/core/net/chat-websocket.ts index 30a591b0..81ced9b0 100644 --- a/src/core/net/chat-websocket.ts +++ b/src/core/net/chat-websocket.ts @@ -15,6 +15,7 @@ * - onError(errorMessage) */ import { getApiConfig } from "@/core/net/config/api_config"; +import { AppEnvUtil } from "@/utils"; export interface SentencePayload { index: number; @@ -64,7 +65,7 @@ export class ChatWebSocket { if (this.disposed) return; if (this.ws && this.ws.readyState !== WebSocket.CLOSED) return; try { - const url = `${this.serverUrl}?token=${encodeURIComponent(this.token)}`; + const url = buildChatWebSocketUrl(this.serverUrl, this.token); this.ws = new WebSocket(url); this.ws.onopen = () => { // 连接成功;userId 由后端在第一条消息中下发 @@ -173,6 +174,11 @@ export class ChatWebSocket { } } +function buildChatWebSocketUrl(serverUrl: string, token: string): string { + if (AppEnvUtil.isDevelopment()) return serverUrl; + return `${serverUrl}?token=${encodeURIComponent(token)}`; +} + /** 默认创建实例:从 env 读取 WS URL。 */ export function createChatWebSocket(token: string): ChatWebSocket { const base = getApiConfig().wsUrl;