mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
listen for ok and err response when sending advert
This commit is contained in:
parent
4a3554ca36
commit
428eb70b59
1 changed files with 33 additions and 2 deletions
|
|
@ -361,12 +361,43 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
async sendAdvert(type) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
|
||||
// resolve promise when we receive ok
|
||||
const onOk = () => {
|
||||
this.off(Constants.ResponseCodes.Ok, onOk);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
resolve();
|
||||
}
|
||||
|
||||
// reject promise when we receive err
|
||||
const onErr = () => {
|
||||
this.off(Constants.ResponseCodes.Ok, onOk);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
reject();
|
||||
}
|
||||
|
||||
// listen for events
|
||||
this.once(Constants.ResponseCodes.Ok, onOk);
|
||||
this.once(Constants.ResponseCodes.Err, onErr);
|
||||
|
||||
// send advert
|
||||
await this.sendCommandSendSelfAdvert(type);
|
||||
|
||||
} catch(e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async sendFloodAdvert() {
|
||||
await this.sendCommandSendSelfAdvert(Constants.SelfAdvertTypes.Flood);
|
||||
return await this.sendAdvert(Constants.SelfAdvertTypes.Flood);
|
||||
}
|
||||
|
||||
async sendZeroHopAdvert() {
|
||||
await this.sendCommandSendSelfAdvert(Constants.SelfAdvertTypes.ZeroHop);
|
||||
return await this.sendAdvert(Constants.SelfAdvertTypes.ZeroHop);
|
||||
}
|
||||
|
||||
setAdvertName(name) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue