Update Base.ts

This commit is contained in:
Adhiraj
2020-08-04 14:03:01 +05:30
parent a5fffb6218
commit 243461c4c2

View File

@@ -255,6 +255,8 @@ export default class WAConnectionBase {
* @return the message tag * @return the message tag
*/ */
protected async sendBinary(json: WANode, tags: WATag, tag?: string) { 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 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 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 */ /** Send some message to the WhatsApp servers */
protected async send(m) { protected async send(m) {
if (!this.conn) { if (!this.conn) await this.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')
}
}
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
return this.conn.send(m) 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. * Disconnect from the phone. Your auth credentials become invalid after sending a disconnect request.
* @see close() if you just want to close the connection * @see close() if you just want to close the connection