From e0af9f99a078dde479838e977efda16032834a6c Mon Sep 17 00:00:00 2001 From: xingyy <64720302+Concur-max@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:23:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(websocket):=20=E5=AE=8C=E5=96=84=20Web?= =?UTF-8?q?Socket=20=E5=AE=A2=E6=88=B7=E7=AB=AF=20token=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=92=8C=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 WebSocketClient 构造函数中存储 token - 自动在连接参数中添加 token - 添加 WebSocket 消息解析错误的控制台日志 --- app/utils/websocket.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/utils/websocket.ts b/app/utils/websocket.ts index e8d907e..f6be471 100644 --- a/app/utils/websocket.ts +++ b/app/utils/websocket.ts @@ -1,9 +1,11 @@ export class WebSocketClient { private socket: WebSocket | null = null private baseUrl: string + private token: string constructor(baseUrl: string, token: string) { this.baseUrl = baseUrl + this.token = token } connect(path: string, params: Record = {}) { @@ -12,6 +14,7 @@ export class WebSocketClient { // 构建参数对象,自动添加 token const queryParams = { + token: this.token, ...params } @@ -30,12 +33,14 @@ export class WebSocketClient { }, onMessage: (callback: (data: any) => void) => { this.socket!.onmessage = (event) => { + try { const data = JSON.parse(event.data) callback(data) } catch (error) { - } + console.error('解析消息失败:', error) + } } }, onClose: (callback: () => void) => {