Skip to content

Commit

Permalink
fix(ws): disconnect ws before reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
osdio committed Jul 26, 2020
1 parent a3f12d9 commit 54f02f9
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/WS/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class WsRpc extends IPC_WS {
this.headers = options.headers;
this.clientConfig = options.clientConfig;

this._timeout = null;

this.reconnect();

// Try to reconnect.
Expand All @@ -35,14 +37,15 @@ class WsRpc extends IPC_WS {
if (times > options.retryTimes) {
return;
}
setTimeout(() => {
this._timeout = setTimeout(() => {
times++;
this.reconnect();
}, options.retryInterval);
});
}

reconnect() {
this.disconnect();
this.socket = new Websocket(this.path, this.protocol, null, this.headers, null, this.clientConfig);
this.socket.onopen = () => {
(this.socket.readyState === this.socket.OPEN) && this._connected();
Expand All @@ -61,6 +64,8 @@ class WsRpc extends IPC_WS {

disconnect() {
this.socket && this.socket.close && this.socket.close();
clearTimeout(this._timeout);
this.socket = null;
}
}

Expand Down

0 comments on commit 54f02f9

Please sign in to comment.