Skip to content
This repository was archived by the owner on Nov 20, 2019. It is now read-only.

Commit 591cb66

Browse files
author
binsee
committed
fix: 修复重连机制可能引发新的异常问题
并且限制两次调用start的间隔时间需要大于200ms
1 parent 34c6a71 commit 591cb66

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ class Padchat extends EventEmitter {
5656
this.url = url
5757
this._event = new EventEmitter()
5858
// 向ws服务器提交指令后,返回结果的超时时间,单位毫秒
59-
this.sendTimeout = 10 * 1000
60-
this.connected = false
61-
this.ws = {}
59+
this.sendTimeout = 10 * 1000
60+
this.connected = false
61+
this._lastStartTime = 0
62+
this.ws = {}
6263
this.start()
6364
}
6465

@@ -68,8 +69,13 @@ class Padchat extends EventEmitter {
6869
* @memberof Padchat
6970
*/
7071
async start() {
71-
if (this.ws instanceof Websocket) {
72-
this.ws.close()
72+
// 限制启动ws连接间隔时间
73+
if (Date.now() - this._lastStartTime < 200) {
74+
throw new Error('建立ws连接时间间隔过短!')
75+
}
76+
this._lastStartTime = Date.now()
77+
if (this.ws instanceof Websocket && this.ws.readyState === this.ws.OPEN) {
78+
this.ws.terminate()
7379
}
7480
this.ws = new Websocket(this.url)
7581
.on('message', (msg) => {

0 commit comments

Comments
 (0)