fix(chat): omit websocket token in development

This commit is contained in:
2026-06-23 18:35:15 +08:00
parent 18617ac7e5
commit 25b786bcad
+7 -1
View File
@@ -15,6 +15,7 @@
* - onError(errorMessage) * - onError(errorMessage)
*/ */
import { getApiConfig } from "@/core/net/config/api_config"; import { getApiConfig } from "@/core/net/config/api_config";
import { AppEnvUtil } from "@/utils";
export interface SentencePayload { export interface SentencePayload {
index: number; index: number;
@@ -64,7 +65,7 @@ export class ChatWebSocket {
if (this.disposed) return; if (this.disposed) return;
if (this.ws && this.ws.readyState !== WebSocket.CLOSED) return; if (this.ws && this.ws.readyState !== WebSocket.CLOSED) return;
try { try {
const url = `${this.serverUrl}?token=${encodeURIComponent(this.token)}`; const url = buildChatWebSocketUrl(this.serverUrl, this.token);
this.ws = new WebSocket(url); this.ws = new WebSocket(url);
this.ws.onopen = () => { this.ws.onopen = () => {
// 连接成功;userId 由后端在第一条消息中下发 // 连接成功;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。 */ /** 默认创建实例:从 env 读取 WS URL。 */
export function createChatWebSocket(token: string): ChatWebSocket { export function createChatWebSocket(token: string): ChatWebSocket {
const base = getApiConfig().wsUrl; const base = getApiConfig().wsUrl;