mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Reconnect fixes
This commit is contained in:
@@ -73,12 +73,12 @@ export class WAConnection extends EventEmitter {
|
||||
this.registerCallback (['Cmd', 'type:disconnect'], json => this.unexpectedDisconnect(json[1].kind))
|
||||
}
|
||||
async unexpectedDisconnect (error?: DisconnectReason) {
|
||||
const willReconnect = this.autoReconnect === ReconnectMode.onAllErrors || (this.autoReconnect === ReconnectMode.onConnectionLost && (error === 'lost' || error === 'closed'))
|
||||
const willReconnect = this.autoReconnect === ReconnectMode.onAllErrors || (this.autoReconnect === ReconnectMode.onConnectionLost && (error !== 'replaced'))
|
||||
|
||||
this.log (`got disconnected, reason ${error || 'unknown'}${willReconnect ? ', reconnecting in a few seconds...' : ''}`, MessageLogLevel.info)
|
||||
|
||||
this.closeInternal(error, willReconnect)
|
||||
willReconnect && this.reconnectLoop ()
|
||||
|
||||
willReconnect && !this.cancelReconnect && this.reconnectLoop ()
|
||||
}
|
||||
/**
|
||||
* base 64 encode the authentication credentials and return them
|
||||
@@ -285,7 +285,7 @@ export class WAConnection extends EventEmitter {
|
||||
this.pendingRequests.forEach (({reject}) => reject(new Error('closed')))
|
||||
this.pendingRequests = []
|
||||
}
|
||||
protected closeInternal (reason?: DisconnectReason, isReconnecting: boolean = false) {
|
||||
protected closeInternal (reason?: DisconnectReason, isReconnecting: boolean=false) {
|
||||
this.qrTimeout && clearTimeout (this.qrTimeout)
|
||||
this.phoneCheck && clearTimeout (this.phoneCheck)
|
||||
|
||||
@@ -303,8 +303,8 @@ export class WAConnection extends EventEmitter {
|
||||
}
|
||||
})
|
||||
if (this.keepAliveReq) clearInterval(this.keepAliveReq)
|
||||
|
||||
this.emit ('closed', { reason, isReconnecting })
|
||||
// reconnecting if the timeout is active for the reconnect loop
|
||||
this.emit ('closed', { reason, isReconnecting: this.cancelReconnect || isReconnecting })
|
||||
}
|
||||
protected async reconnectLoop () {
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ export class WAConnection extends Base {
|
||||
this.startKeepAliveRequest()
|
||||
|
||||
this.conn.removeAllListeners ('error')
|
||||
this.conn.removeAllListeners ('close')
|
||||
this.conn.on ('close', () => this.unexpectedDisconnect ('closed'))
|
||||
|
||||
this.state = 'open'
|
||||
@@ -37,7 +38,8 @@ export class WAConnection extends Base {
|
||||
})
|
||||
this.conn.on('message', m => this.onMessageRecieved(m))
|
||||
// if there was an error in the WebSocket
|
||||
this.conn.on('error', error => { this.closeInternal(error.message as any); reject(error) })
|
||||
this.conn.on('error', reject)
|
||||
this.conn.on('close', () => reject(new Error('closed')))
|
||||
})
|
||||
|
||||
try {
|
||||
@@ -77,7 +79,6 @@ export class WAConnection extends Base {
|
||||
let receivedMessages = false
|
||||
let convoResolve: () => void
|
||||
|
||||
this.log('waiting for chats & contacts', MessageLogLevel.info) // wait for the message with chats
|
||||
const waitForConvos = () =>
|
||||
Utils.promiseTimeout(timeoutMs, resolve => {
|
||||
convoResolve = () => {
|
||||
@@ -91,7 +92,7 @@ export class WAConnection extends Base {
|
||||
}
|
||||
const chatUpdate = json => {
|
||||
receivedMessages = true
|
||||
const isLast = json[1].last || (json[1].add === 'last' && stopAfterMostRecentMessage)
|
||||
const isLast = json[1].last || stopAfterMostRecentMessage
|
||||
const messages = json[2] as WANode[]
|
||||
|
||||
if (messages) {
|
||||
@@ -133,6 +134,8 @@ export class WAConnection extends Base {
|
||||
|
||||
this.deregisterCallback(['response', 'type:chat'])
|
||||
|
||||
this.log ('received chats list', MessageLogLevel.info)
|
||||
|
||||
if (this.chats.all().length > 0) waitForConvos().then (resolve)
|
||||
else resolve ()
|
||||
})
|
||||
@@ -157,6 +160,8 @@ export class WAConnection extends Base {
|
||||
resolve ()
|
||||
|
||||
this.deregisterCallback(['response', 'type:contacts'])
|
||||
|
||||
this.log ('received contacts list', MessageLogLevel.info)
|
||||
})
|
||||
})
|
||||
)
|
||||
@@ -259,6 +264,7 @@ export class WAConnection extends Base {
|
||||
try {
|
||||
await this.connect ()
|
||||
this.cancelReconnect = null
|
||||
break
|
||||
} catch (error) {
|
||||
this.log (`error in reconnecting: ${error}, reconnecting...`, MessageLogLevel.info)
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ export class WAConnection extends Base {
|
||||
}
|
||||
protected registerPhoneConnectionPoll () {
|
||||
this.phoneCheck = setInterval (() => {
|
||||
this.checkPhoneConnection (7500) // 7500 ms for timeout
|
||||
this.checkPhoneConnection (5000) // 5000 ms for timeout
|
||||
.then (connected => {
|
||||
if (this.phoneConnected != connected) {
|
||||
this.emit ('connection-phone-change', {connected})
|
||||
@@ -269,7 +269,7 @@ export class WAConnection extends Base {
|
||||
this.phoneConnected = connected
|
||||
})
|
||||
.catch (error => this.log(`error in getting phone connection: ${error}`, MessageLogLevel.info))
|
||||
}, 20000)
|
||||
}, 15000)
|
||||
}
|
||||
|
||||
// Add all event types
|
||||
|
||||
@@ -134,7 +134,7 @@ export class WAConnection extends Base {
|
||||
/** Get the invite link of the given group */
|
||||
async groupInviteCode(jid: string) {
|
||||
const json = ['query', 'inviteCode', jid]
|
||||
const response = await this.query({json})
|
||||
const response = await this.query({json, expect200: true})
|
||||
return response.code as string
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user