Documented disconnect reason

This commit is contained in:
Adhiraj
2020-08-27 16:36:14 +05:30
parent dd91c9a0d9
commit 2ce9a1c28d
4 changed files with 32 additions and 12 deletions

View File

@@ -76,10 +76,10 @@ export class WAConnection extends EventEmitter {
async unexpectedDisconnect (error: DisconnectReason) {
const willReconnect =
(this.autoReconnect === ReconnectMode.onAllErrors ||
(this.autoReconnect === ReconnectMode.onConnectionLost && (error !== 'replaced'))) &&
error !== 'invalid_session'
(this.autoReconnect === ReconnectMode.onConnectionLost && (error !== DisconnectReason.replaced))) &&
error !== DisconnectReason.invalidSession // do not reconnect if credentials have been invalidated
this.closeInternal(error, willReconnect)
willReconnect && !this.cancelReconnect && this.reconnectLoop ()
}
/**
@@ -215,7 +215,7 @@ export class WAConnection extends EventEmitter {
const response = await this.waitForMessage(tag, json, timeoutMs)
if (expect200 && response.status && Math.floor(+response.status / 100) !== 2) {
if (response.status >= 500) {
this.unexpectedDisconnect ('bad_session')
this.unexpectedDisconnect (DisconnectReason.badSession)
const response = await this.query ({json, binaryTags, tag, timeoutMs, expect200, waitForOpen})
return response
}
@@ -286,7 +286,7 @@ export class WAConnection extends EventEmitter {
}
/** Close the connection to WhatsApp Web */
close () {
this.closeInternal ('intentional')
this.closeInternal (DisconnectReason.intentional)
this.cancelReconnect && this.cancelReconnect ()
}
protected closeInternal (reason?: DisconnectReason, isReconnecting: boolean=false) {

View File

@@ -1,5 +1,5 @@
import * as Utils from './Utils'
import { WAMessage, WAChat, WAContact, MessageLogLevel, WANode, KEEP_ALIVE_INTERVAL_MS, BaileysError, WAConnectOptions } from './Constants'
import { WAMessage, WAChat, WAContact, MessageLogLevel, WANode, KEEP_ALIVE_INTERVAL_MS, BaileysError, WAConnectOptions, DisconnectReason } from './Constants'
import {WAConnection as Base} from './1.Validation'
import Decoder from '../Binary/Decoder'
@@ -26,7 +26,7 @@ export class WAConnection extends Base {
this.startKeepAliveRequest()
this.conn.removeAllListeners ('error')
this.conn.removeAllListeners ('close')
this.conn.on ('close', () => this.unexpectedDisconnect ('close'))
this.conn.on ('close', () => this.unexpectedDisconnect (DisconnectReason.close))
})
.then (resolve)
.catch (reject)
@@ -249,7 +249,7 @@ export class WAConnection extends Base {
check if it's been a suspicious amount of time since the server responded with our last seen
it could be that the network is down
*/
if (diff > KEEP_ALIVE_INTERVAL_MS+5000) this.unexpectedDisconnect ('lost')
if (diff > KEEP_ALIVE_INTERVAL_MS+5000) this.unexpectedDisconnect (DisconnectReason.lost)
else this.send ('?,,') // if its all good, send a keep alive request
}, KEEP_ALIVE_INTERVAL_MS)
}
@@ -266,7 +266,9 @@ export class WAConnection extends Base {
await delay
try {
const reconnectID = this.lastDisconnectReason !== 'replaced' && this.lastDisconnectReason !== 'unknown' && this.user ? this.user.id.replace ('@s.whatsapp.net', '@c.us') : null
const shouldUseReconnect = this.lastDisconnectReason !== DisconnectReason.replaced && this.lastDisconnectReason !== DisconnectReason.unknown && this.user
const reconnectID = shouldUseReconnect ? this.user.id.replace ('@s.whatsapp.net', '@c.us') : null
await this.connect ({ timeoutMs: 30000, retryOnNetworkErrors: true, reconnectID })
this.cancelReconnect = null
break

View File

@@ -1,6 +1,6 @@
import * as QR from 'qrcode-terminal'
import { WAConnection as Base } from './3.Connect'
import { MessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, MessageLogLevel, PresenceUpdate, BaileysEvent } from './Constants'
import { MessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, MessageLogLevel, PresenceUpdate, BaileysEvent, DisconnectReason } from './Constants'
import { whatsappID, unixTimestampSeconds, isGroupID } from './Utils'
export class WAConnection extends Base {
@@ -273,7 +273,7 @@ export class WAConnection extends Base {
/** when the connection is opening */
on (event: 'connecting', listener: () => void): this
/** when the connection has closed */
on (event: 'close', listener: (err: {reason?: string, isReconnecting: boolean}) => void): this
on (event: 'close', listener: (err: {reason?: DisconnectReason | string, isReconnecting: boolean}) => void): this
/** when a new QR is generated, ready for scanning */
on (event: 'qr', listener: (qr: string) => void): this
/** when the connection to the phone changes */

View File

@@ -67,7 +67,25 @@ export type WAConnectOptions = {
}
export type WAConnectionState = 'open' | 'connecting' | 'close'
export type DisconnectReason = 'close' | 'lost' | 'replaced' | 'intentional' | 'invalid_session' | 'unknown' | 'bad_session'
/** Types of Disconnect Reasons */
export enum DisconnectReason {
/** The connection was closed intentionally */
intentional = 'intentional',
/** The connection was terminated either by the client or server */
close = 'close',
/** The connection was lost, called when the server stops responding to requests */
lost = 'lost',
/** When WA Web is opened elsewhere & this session is disconnected */
replaced = 'intentional',
/** The credentials for the session have been invalidated, i.e. logged out either from the phone or WA Web */
invalidSession = 'invalid_session',
/** Received a 500 result in a query -- something has gone very wrong */
badSession = 'bad_session',
/** No idea, can be a sign of log out too */
unknown = 'unknown',
/** Well, the connection timed out */
timedOut = 'timed out'
}
export enum MessageLogLevel {
none=0,
info=1,