fix(chat): omit websocket token in development
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user