diff --git a/index.html b/index.html index e32dfa7..3af68df 100644 --- a/index.html +++ b/index.html @@ -291,15 +291,9 @@ return; } - // compose message - const txtType = Constants.TxtTypes.Plain; - const attempt = 0; - const senderTimestamp = Math.floor(Date.now() / 1000); - const pubKeyPrefix = contact.publicKey; - const text = message; - // send message - await this.connection.sendCommandSendTxtMsg(txtType, attempt, senderTimestamp, pubKeyPrefix, text); + const response = await this.connection.sendTextMessage(contact.publicKey, message); + console.log(response); }, async resetPath(contact) { diff --git a/src/connection/connection.js b/src/connection/connection.js index 16a1b3c..759c414 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -229,7 +229,8 @@ class Connection extends EventEmitter { onSentResponse(bufferReader) { this.emit(Constants.ResponseCodes.Sent, { - + expectedAckCrc: bufferReader.readUInt32LE(), + estTimeout: bufferReader.readUInt32LE(), }); } @@ -310,6 +311,29 @@ class Connection extends EventEmitter { }); } + sendTextMessage(contactPublicKey, text) { + return new Promise(async (resolve, reject) => { + try { + + // resolve with the first sent response + this.once(Constants.ResponseCodes.Sent, (response) => { + resolve(response); + }); + + // compose message + const txtType = Constants.TxtTypes.Plain; + const attempt = 0; + const senderTimestamp = Math.floor(Date.now() / 1000); + + // send message + await this.sendCommandSendTxtMsg(txtType, attempt, senderTimestamp, contactPublicKey, text); + + } catch(e) { + reject(e); + } + }); + } + } export default Connection;