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