implement channel.send

pull/1/head
ansuz 8 years ago
parent d835005905
commit c63807fd2b

@ -372,6 +372,8 @@ define([
messenger.input = $input[0]; messenger.input = $input[0];
var send = function () { var send = function () {
// TODO implement sending queue
// TODO separate message logic from UI
var channel = channels[data.channel]; var channel = channels[data.channel];
if (channel.sending) { if (channel.sending) {
console.error("still sending"); console.error("still sending");
@ -385,34 +387,18 @@ define([
console.error("input is disabled"); console.error("input is disabled");
return; return;
} }
// Send the message
var msg = [Types.message, proxy.curvePublic, +new Date(), $input.val()];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.sending = true;
console.log(channel.wc); var payload = $input.val();
var network = common.getNetwork(); // Send the message
if (!network.webChannels.some(function (wc) { channel.send(payload, function (e) {
if (wc.id === channel.wc.id) { if (e) {
console.error(wc.id, channel.wc.id); channel.sending = false;
return true; console.error(err);
}
console.error(wc.id, channel.wc.id);
//return wc.id === channel.wc.id;
})) {
console.error('no such channel:' + channel.wc.id);
return; return;
} }
channel.wc.bcast(cryptMsg).then(function () {
$input.val(''); $input.val('');
pushMsg(common, channel, cryptMsg);
channel.refresh(); channel.refresh();
channel.sending = false; channel.sending = false;
}, function (err) {
channel.sending = false;
console.error(err);
}); });
}; };
$('<button>', {'class': 'btn btn-primary fa fa-paper-plane'}) $('<button>', {'class': 'btn btn-primary fa fa-paper-plane'})
@ -710,6 +696,25 @@ define([
var messageHash = oldestMessage[0]; var messageHash = oldestMessage[0];
getMoreHistory(network, chan, messageHash, 10); getMoreHistory(network, chan, messageHash, 10);
}, },
send: function (payload, cb) {
if (!network.webChannels.some(function (wc) {
if (wc.id === channel.wc.id) { return true; }
})) {
return void cb('NO_SUCH_CHANNEL');
}
var msg = [Types.message, proxy.curvePublic, +new Date(), payload];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.sending = true;
channel.wc.bcast(cryptMsg).then(function () {
pushMsg(common, channel, cryptMsg);
cb();
}, function (err) {
cb(err);
});
},
}; };
chan.on('message', function (msg, sender) { chan.on('message', function (msg, sender) {
onMessage(common, msg, sender, chan); onMessage(common, msg, sender, chan);

Loading…
Cancel
Save