connect cooldown + terminate WS instead of close

This commit is contained in:
Adhiraj
2020-09-01 13:16:50 +05:30
parent 218c9bcc18
commit 50d50e4ae9
5 changed files with 29 additions and 57 deletions

View File

@@ -42,7 +42,8 @@ export class WAConnection extends EventEmitter {
connectOptions: WAConnectOptions = {
timeoutMs: 60*1000,
waitForChats: true,
maxRetries: 5
maxRetries: 5,
connectCooldownMs: 5000
}
/** When to auto-reconnect */
autoReconnect = ReconnectMode.onConnectionLost
@@ -70,7 +71,8 @@ export class WAConnection extends EventEmitter {
protected lastSeen: Date = null // last keep alive received
protected qrTimeout: NodeJS.Timeout
protected lastDisconnectReason: DisconnectReason
protected lastConnectTime: Date = null
protected lastDisconnectReason: DisconnectReason
constructor () {
super ()
@@ -311,7 +313,6 @@ export class WAConnection extends EventEmitter {
this.msgCount = 0
this.phoneConnected = false
this.lastDisconnectReason = reason
this.endConnection ()
@@ -319,14 +320,14 @@ export class WAConnection extends EventEmitter {
this.pendingRequests.forEach (({reject}) => reject(new Error('close')))
this.pendingRequests = []
}
// reconnecting if the timeout is active for the reconnect loop
this.emit ('close', { reason, isReconnecting })
}
protected endConnection () {
this.conn?.removeAllListeners ('close')
this.conn?.close()
this.conn?.removeAllListeners ('message')
this.conn?.close ()
this.conn?.terminate()
this.conn = null
this.lastSeen = null