add sendJsonMessage
This commit is contained in:
parent
bdc22cbbd4
commit
8bccb22c20
69
src/index.js
69
src/index.js
|
@ -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([
|
||||||
|
@ -55,9 +55,9 @@ class StatusJS {
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(channelName, cb) {
|
onMessage(channelName, cb) {
|
||||||
if (!this.channels[channelName]) {
|
if (!this.channels[channelName]) {
|
||||||
return cb("unknown channel: " + channelName);
|
return cb("unknown channel: " + channelName);
|
||||||
}
|
}
|
||||||
this.channels[channelName].subscription = this.shh.subscribe("messages", {
|
this.channels[channelName].subscription = this.shh.subscribe("messages", {
|
||||||
minPow: POW_TARGET,
|
minPow: POW_TARGET,
|
||||||
symKeyID: this.channels[channelName].channelKey,
|
symKeyID: this.channels[channelName].channelKey,
|
||||||
|
@ -70,26 +70,47 @@ class StatusJS {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(channelName, msg, cb) {
|
sendMessage(channelName, msg, cb) {
|
||||||
if (!this.channels[channelName]) {
|
if (!this.channels[channelName]) {
|
||||||
return cb("unknown channel: " + channelName);
|
return cb("unknown channel: " + channelName);
|
||||||
}
|
}
|
||||||
this.shh.post({
|
this.shh.post({
|
||||||
symKeyID: this.channels[channelName].channelKey,
|
symKeyID: this.channels[channelName].channelKey,
|
||||||
sig: this.sig,
|
sig: this.sig,
|
||||||
ttl: TTL,
|
ttl: TTL,
|
||||||
topic: this.channels[channelName].channelCode,
|
topic: this.channels[channelName].channelCode,
|
||||||
payload: createStatusPayload(msg),
|
payload: createStatusPayload(msg),
|
||||||
powTime: POW_TIME,
|
powTime: POW_TIME,
|
||||||
powTarget: POW_TARGET
|
powTarget: POW_TARGET
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
if (!cb) return;
|
if (!cb) return;
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
if (!cb) return;
|
if (!cb) return;
|
||||||
cb(e, false);
|
cb(e, false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue