add sendJsonMessage

This commit is contained in:
Iuri Matias 2018-11-07 13:18:20 -05:00
parent bdc22cbbd4
commit 8bccb22c20
1 changed files with 45 additions and 24 deletions

View File

@ -6,12 +6,12 @@ const POW_TIME = 1;
const TTL = 10; const TTL = 10;
const POW_TARGET = 0.002; const POW_TARGET = 0.002;
function createStatusPayload(msg) { function createStatusPayload(msg, isJson) {
let tag = '~#c4'; let tag = '~#c4';
let content = msg; let content = msg;
let messageType = '~:public-group-user-message'; let messageType = '~:public-group-user-message';
let clockValue = (new Date().getTime()) * 100; let clockValue = (new Date().getTime()) * 100;
let contentType = 'text/plain'; let contentType = (isJson ? 'content/json' : 'text/plain');
let timestamp = new Date().getTime(); let timestamp = new Date().getTime();
return asciiToHex( return asciiToHex(
JSON.stringify([ JSON.stringify([
@ -91,6 +91,27 @@ class StatusJS {
}); });
} }
sendJsonMessage(channelName, msg, cb) {
if (!this.channels[channelName]) {
return cb("unknown channel: " + channelName);
}
this.shh.post({
symKeyID: this.channels[channelName].channelKey,
sig: this.sig,
ttl: TTL,
topic: this.channels[channelName].channelCode,
payload: createStatusPayload(JSON.stringify(msg), true),
powTime: POW_TIME,
powTarget: POW_TARGET
}).then(() => {
if (!cb) return;
cb(null, true);
}).catch((e) => {
if (!cb) return;
cb(e, false);
});
}
} }
module.exports = StatusJS; module.exports = StatusJS;