mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
connect cooldown + terminate WS instead of close
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user