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

View File

@@ -18,8 +18,11 @@ export class WAConnection extends Base {
while (this.state === 'connecting') {
tries += 1
try {
// if the first try failed, delay & connect again
await this.connectInternal (options, tries > 1 && 2000)
const diff = this.lastConnectTime ? new Date().getTime()-this.lastConnectTime.getTime() : Infinity
await this.connectInternal (
options,
diff > this.connectOptions.connectCooldownMs ? 0 : this.connectOptions.connectCooldownMs
)
this.phoneConnected = true
this.state = 'open'
@@ -34,6 +37,8 @@ export class WAConnection extends Base {
}
if (!willReconnect) throw error
} finally {
this.lastConnectTime = new Date()
}
}

View File

@@ -65,6 +65,8 @@ export type WAConnectOptions = {
maxRetries?: number
/** should the chats be waited for */
waitForChats?: boolean
connectCooldownMs?: number
}
export type WAConnectionState = 'open' | 'connecting' | 'close'

View File

@@ -123,7 +123,7 @@ export const openWebSocketConnection = (timeoutMs: number, retryOnNetworkError:
try {
const ws = await newWS()
if (cancelled) {
ws.close ()
ws.terminate ()
break
} else return ws
} catch (error) {