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

Commit

Permalink
fix: 修复重连机制可能引发新的异常问题
Browse files Browse the repository at this point in the history
并且限制两次调用start的间隔时间需要大于200ms
  • Loading branch information
binsee committed Jun 13, 2018
1 parent 34c6a71 commit 591cb66
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ class Padchat extends EventEmitter {
this.url = url
this._event = new EventEmitter()
// 向ws服务器提交指令后,返回结果的超时时间,单位毫秒
this.sendTimeout = 10 * 1000
this.connected = false
this.ws = {}
this.sendTimeout = 10 * 1000
this.connected = false
this._lastStartTime = 0
this.ws = {}
this.start()
}

Expand All @@ -68,8 +69,13 @@ class Padchat extends EventEmitter {
* @memberof Padchat
*/
async start() {
if (this.ws instanceof Websocket) {
this.ws.close()
// 限制启动ws连接间隔时间
if (Date.now() - this._lastStartTime < 200) {
throw new Error('建立ws连接时间间隔过短!')
}
this._lastStartTime = Date.now()
if (this.ws instanceof Websocket && this.ws.readyState === this.ws.OPEN) {
this.ws.terminate()
}
this.ws = new Websocket(this.url)
.on('message', (msg) => {
Expand Down

0 comments on commit 591cb66

Please sign in to comment.