From 69bae32c80ca5b3816e38919dc15345292c92c90 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 22 Apr 2025 11:43:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ws=E5=BF=83=E8=B7=B3=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/ws-websocket.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/plugins/ws-websocket.ts b/src/plugins/ws-websocket.ts index 771efee2..623cd7cf 100644 --- a/src/plugins/ws-websocket.ts +++ b/src/plugins/ws-websocket.ts @@ -119,17 +119,15 @@ export class WS { }; // 用于指定当从服务器接受到信息时的回调函数。 ws.onmessage = ev => { + if (ev.type !== 'message') return; // 解析文本消息 - if (ev.type === 'message') { - const data = ev.data; - try { - const jsonData = JSON.parse(data); - if (typeof options.onmessage === 'function') { - options.onmessage(jsonData); - } - } catch (error) { - console.error('websocket message formatting error', error); + try { + const jsonData = JSON.parse(ev.data); + if (typeof options.onmessage === 'function') { + options.onmessage(jsonData); } + } catch (error) { + console.error('websocket message formatting error', error); } }; // 用于指定连接关闭后的回调函数。 @@ -221,7 +219,7 @@ export class WS { this.heartInterval = window.setInterval(() => { this.send({ requestId: `${Date.now()}`, - type: 'ping', + type: 'PING', }); }, heartTimer); }