From d7e08a1fff1a148e43378d6beac1ce1f7c7c2011 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Wed, 28 Feb 2024 17:37:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ws=E5=B7=A5=E5=85=B7=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/ws-websocket.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/plugins/ws-websocket.ts b/src/plugins/ws-websocket.ts index 5fbaa54e..95f668d1 100644 --- a/src/plugins/ws-websocket.ts +++ b/src/plugins/ws-websocket.ts @@ -160,11 +160,15 @@ export class WS { } } - // 发送消息 - public send(data: Record) { + /** + * 发送消息 + * @param data JSON数据 + * @returns + */ + public send(data: Record): boolean { if (!this.ws) { console.warn('websocket unavailable'); - return; + return false; } // 非正常状态关闭 @@ -173,7 +177,7 @@ export class WS { this.ws.readyState === WebSocket.CLOSING ) { this.close(); - return; + return false; } // 正在连接时 if (this.ws.readyState === WebSocket.CONNECTING) { @@ -184,7 +188,21 @@ export class WS { // 在连接的 if (this.ws.readyState === WebSocket.OPEN) { this.ws.send(JSON.stringify(data)); + return true; } + return false; + } + + /**连接状态 + * + * WebSocket.OPEN + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/readyState) + */ + public state(): number { + if (!this.ws) { + return -1; + } + return this.ws.readyState; } // 手动关闭socket