mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add send text message function
This commit is contained in:
parent
8dc58d60c4
commit
272ece07e8
2 changed files with 27 additions and 9 deletions
10
index.html
10
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) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue