From d55a4e3da7c5b85b9b203aab435351e0131edd25 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 26 Feb 2024 12:09:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ws=E5=87=BD=E6=95=B0=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=AD=A3=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/ws-websocket.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/plugins/ws-websocket.ts b/src/plugins/ws-websocket.ts index 6bf9e099..5fbaa54e 100644 --- a/src/plugins/ws-websocket.ts +++ b/src/plugins/ws-websocket.ts @@ -11,7 +11,7 @@ export type OptionsType = { /**地址栏参数 */ params?: Record; /**onopen事件的回调函数 */ - onopen?: () => void; + onopen?: Function; /**message事件的回调函数 */ onmessage: (data: Record) => void; /**error事件的回调函数 */ @@ -114,7 +114,7 @@ export class WS { this.heartCheck(options.heartTimer); } if (typeof options.onopen === 'function') { - options.onopen(); + options.onopen(ev); } }; // 用于指定当从服务器接受到信息时的回调函数。 @@ -166,7 +166,7 @@ export class WS { console.warn('websocket unavailable'); return; } - + // 非正常状态关闭 if ( this.ws.readyState === WebSocket.CLOSED || @@ -175,8 +175,16 @@ export class WS { this.close(); return; } - - this.ws.send(JSON.stringify(data)); + // 正在连接时 + if (this.ws.readyState === WebSocket.CONNECTING) { + setTimeout(() => { + this.ws && this.ws.send(JSON.stringify(data)); + }, 1000); + } + // 在连接的 + if (this.ws.readyState === WebSocket.OPEN) { + this.ws.send(JSON.stringify(data)); + } } // 手动关闭socket