Added reconnect mechanism

This commit is contained in:
Adhiraj
2020-08-25 00:26:47 +05:30
parent e2372bf60b
commit 68b64b9707
5 changed files with 38 additions and 21 deletions

View File

@@ -20,8 +20,8 @@ export class WAConnection extends Base {
ws
.then (conn => this.conn = conn)
.then (() => this.conn.on('message', data => this.onMessageRecieved(data as any)))
.then (() => this.log('connected to WhatsApp Web server, authenticating...', MessageLogLevel.info))
.then (() => this.authenticate())
.then (() => this.log(`connected to WhatsApp Web server, authenticating via ${options.reconnectID ? 'reconnect' : 'takeover'}`, MessageLogLevel.info))
.then (() => this.authenticate(options?.reconnectID))
.then (() => {
this.startKeepAliveRequest()
this.conn.removeAllListeners ('error')
@@ -261,7 +261,8 @@ export class WAConnection extends Base {
await delay
try {
await this.connect ({ timeoutMs: 30000, retryOnNetworkErrors: true })
const reconnectID = this.lastDisconnectReason !== 'replaced' && this.user ? this.user.id.replace ('@s.whatsapp.net', '@c.us') : null
await this.connect ({ timeoutMs: 30000, retryOnNetworkErrors: true, reconnectID })
this.cancelReconnect = null
break
} catch (error) {