From 243461c4c2b43b3370129dbb788de78bf98eb085 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Tue, 4 Aug 2020 14:03:01 +0530 Subject: [PATCH] Update Base.ts --- src/WAConnection/Base.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/WAConnection/Base.ts b/src/WAConnection/Base.ts index d7dd5f8..0770023 100644 --- a/src/WAConnection/Base.ts +++ b/src/WAConnection/Base.ts @@ -255,6 +255,8 @@ export default class WAConnectionBase { * @return the message tag */ protected async sendBinary(json: WANode, tags: WATag, tag?: string) { + if (!this.conn) await this.waitForConnection () + const binary = this.encoder.write(json) // encode the JSON to the WhatsApp binary format let buff = Utils.aesEncrypt(binary, this.authInfo.encKey) // encrypt it using AES and our encKey @@ -282,18 +284,20 @@ export default class WAConnectionBase { } /** Send some message to the WhatsApp servers */ protected async send(m) { - if (!this.conn) { - const timeout = this.pendingRequestTimeoutMs - try { - const task = new Promise (resolve => this.pendingRequests.push(resolve)) - await Utils.promiseTimeout (timeout, task) - } catch { - throw new Error('cannot send message, disconnected from WhatsApp') - } - } + if (!this.conn) await this.waitForConnection () + this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages return this.conn.send(m) } + protected async waitForConnection () { + const timeout = this.pendingRequestTimeoutMs + try { + const task = new Promise (resolve => this.pendingRequests.push(resolve)) + await Utils.promiseTimeout (timeout, task) + } catch { + throw new Error('cannot send message, disconnected from WhatsApp') + } + } /** * Disconnect from the phone. Your auth credentials become invalid after sending a disconnect request. * @see close() if you just want to close the connection