From 2ce9a1c28d341cd6602796343e93c4d4ccb5073e Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Thu, 27 Aug 2020 16:36:14 +0530 Subject: [PATCH] Documented disconnect reason --- src/WAConnection/0.Base.ts | 10 +++++----- src/WAConnection/3.Connect.ts | 10 ++++++---- src/WAConnection/4.Events.ts | 4 ++-- src/WAConnection/Constants.ts | 20 +++++++++++++++++++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index c039287..be31954 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -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) { diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index 0244bda..1562235 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -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 diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index a28f462..2f3907b 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -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 */ diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 30f0315..3632be5 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -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,