Implement reconnect function in the messenger

pull/1/head
yflory 6 years ago
parent 1b2f47c0c0
commit d2c1ed5eff

@ -76,6 +76,12 @@ define([
messenger.handlers[type].forEach(g); messenger.handlers[type].forEach(g);
}; };
var emit = function (ev, data) {
eachHandler('event', function (f) {
f(ev, data);
});
};
messenger.on = function (type, f) { messenger.on = function (type, f) {
var stack = messenger.handlers[type]; var stack = messenger.handlers[type];
if (!Array.isArray(stack)) { if (!Array.isArray(stack)) {
@ -575,72 +581,85 @@ define([
var openChannel = function (data) { var openChannel = function (data) {
var keys = data.keys; var keys = data.keys;
var encryptor = data.encryptor || Curve.createEncryptor(keys); var encryptor = data.encryptor || Curve.createEncryptor(keys);
network.join(data.channel).then(function (chan) { var channel = {
var channel = channels[data.channel] = { id: data.channel,
id: data.channel, isFriendChat: data.isFriendChat,
isFriendChat: data.isFriendChat, isPadChat: data.isPadChat,
isPadChat: data.isPadChat, sending: false,
sending: false, encryptor: encryptor,
encryptor: encryptor, messages: [],
messages: [], userList: [],
wc: chan, mapId: {},
userList: [], };
mapId: {},
var onJoining = function (peer) {
if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer);
// Join event will be sent once we are able to ID this peer
var myData = createData(proxy);
delete myData.channel;
var msg = [Types.mapId, myData, channel.wc.myID];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
var data = {
channel: channel.id,
msg: cryptMsg
}; };
network.sendto(peer, JSON.stringify(data));
};
var onLeaving = function (peer) {
var i = channel.userList.indexOf(peer);
while (i !== -1) {
channel.userList.splice(i, 1);
i = channel.userList.indexOf(peer);
}
// update status
var otherData = channel.mapId[peer];
if (!otherData) { return; }
// Make sure the leaving user is not connected with another netflux id
if (channel.userList.some(function (nId) {
return channel.mapId[nId]
&& channel.mapId[nId].curvePublic === otherData.curvePublic;
})) { return; }
// Send the notification
eachHandler('leave', function (f) {
f(otherData, channel.id);
});
};
var onOpen = function (chan) {
channel.wc = chan;
channels[data.channel] = channel;
chan.on('message', function (msg, sender) { chan.on('message', function (msg, sender) {
onMessage(msg, sender, chan); onMessage(msg, sender, chan);
}); });
var onJoining = function (peer) {
if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer);
// Join event will be sent once we are able to ID this peer
var myData = createData(proxy);
delete myData.channel;
var msg = [Types.mapId, myData, chan.myID];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
var data = {
channel: channel.id,
msg: cryptMsg
};
network.sendto(peer, JSON.stringify(data));
};
chan.members.forEach(function (peer) { chan.members.forEach(function (peer) {
if (peer === Msg.hk) { return; } if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; } if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer); channel.userList.push(peer);
}); });
chan.on('join', onJoining); chan.on('join', onJoining);
chan.on('leave', function (peer) { chan.on('leave', onLeaving);
var i = channel.userList.indexOf(peer);
while (i !== -1) {
channel.userList.splice(i, 1);
i = channel.userList.indexOf(peer);
}
// update status
var otherData = channel.mapId[peer];
if (!otherData) { return; }
// Make sure the leaving user is not connected with another netflux id
if (channel.userList.some(function (nId) {
return channel.mapId[nId]
&& channel.mapId[nId].curvePublic === otherData.curvePublic;
})) { return; }
// Send the notification
eachHandler('leave', function (f) {
f(otherData, channel.id);
});
});
// FIXME don't subscribe to the channel implicitly // FIXME don't subscribe to the channel implicitly
getChannelMessagesSince(channel, data, keys); getChannelMessagesSince(channel, data, keys);
}, function (err) { };
network.join(data.channel).then(onOpen, function (err) {
console.error(err); console.error(err);
}); });
network.on('reconnect', function () {
if (!channels[data.channel]) { return; }
network.join(data.channel).then(onOpen, function (err) {
console.error(err);
});
});
}; };
messenger.getFriendList = function (cb) { messenger.getFriendList = function (cb) {
@ -805,9 +824,7 @@ define([
// TODO load rooms // TODO load rooms
}).nThen(function () { }).nThen(function () {
ready = true; ready = true;
eachHandler('event', function (f) { emit('READY');
f('READY');
});
}); });
}; };
init(); init();
@ -883,9 +900,7 @@ define([
var openPadChat = function (data, cb) { var openPadChat = function (data, cb) {
var channel = data.channel; var channel = data.channel;
if (getChannel(channel)) { if (getChannel(channel)) {
eachHandler('event', function (f) { emit('PADCHAT_READY', channel);
f('PADCHAT_READY', channel);
});
return void cb(); return void cb();
} }
var keys = data.secret && data.secret.keys; var keys = data.secret && data.secret.keys;
@ -901,13 +916,18 @@ define([
}; };
openChannel(chanData); openChannel(chanData);
joining[channel] = function () { joining[channel] = function () {
eachHandler('event', function (f) { emit('PADCHAT_READY', channel);
f('PADCHAT_READY', channel);
});
}; };
cb(); cb();
}; };
network.on('disconnect', function () {
emit('DISCONNECT');
});
network.on('reconnect', function () {
emit('RECONNECT');
});
messenger.execCommand = function (obj, cb) { messenger.execCommand = function (obj, cb) {
var cmd = obj.cmd; var cmd = obj.cmd;
var data = obj.data; var data = obj.data;

@ -122,6 +122,12 @@ define([
find.inList(id).removeClass('cp-app-contacts-notify'); find.inList(id).removeClass('cp-app-contacts-notify');
}; };
var onResize = function () {
// Don't update the width if we are in the contacts app
if (!toolbar) { return; }
var w = $userlist[0].offsetWidth - $userlist[0].clientWidth;
$userlist.css('width', (68 + w)+'px');
};
var reorderRooms = function () { var reorderRooms = function () {
var channels = Object.keys(state.channels).sort(function (a, b) { var channels = Object.keys(state.channels).sort(function (a, b) {
var m1 = state.channels[a].messages.slice(-1)[0]; var m1 = state.channels[a].messages.slice(-1)[0];
@ -136,14 +142,10 @@ define([
}); });
// Make sure the width is correct even if there is a scrollbar // Make sure the width is correct even if there is a scrollbar
var w = $userlist[0].offsetWidth - $userlist[0].clientWidth; onResize();
$userlist.css('width', (68 + w)+'px');
}; };
$(window).on('resize', function () { $(window).on('resize', onResize);
var w = $userlist[0].offsetWidth - $userlist[0].clientWidth;
$userlist.css('width', (68 + w)+'px');
});
var m = function (md) { var m = function (md) {
var d = h('div.cp-app-contacts-content'); var d = h('div.cp-app-contacts-content');
@ -174,7 +176,8 @@ define([
markup.message = function (msg) { markup.message = function (msg) {
if (msg.type !== 'MSG') { return; } if (msg.type !== 'MSG') { return; }
var curvePublic = msg.author; var curvePublic = msg.author;
var name = msg.name || contactsData[msg.author].displayName; var name = typeof msg.name !== "undefined" ? (msg.name || Messages.anonymous)
: contactsData[msg.author].displayName;
var d = msg.time ? new Date(msg.time) : undefined; var d = msg.time ? new Date(msg.time) : undefined;
var day = d ? d.toLocaleDateString() : ''; var day = d ? d.toLocaleDateString() : '';
var hour = d ? d.toLocaleTimeString() : ''; var hour = d ? d.toLocaleTimeString() : '';
@ -770,6 +773,15 @@ define([
$container.removeClass('cp-app-contacts-initializing'); $container.removeClass('cp-app-contacts-initializing');
}; };
var onDisconnect = function () {
debug('disconnected');
$messages.find('.cp-app-contacts-input textarea').prop('disabled', true);
};
var onReconnect = function () {
debug('reconnected');
$messages.find('.cp-app-contacts-input textarea').prop('disabled', false);
};
// Initialize chat when outer is ready (all channels loaded) // Initialize chat when outer is ready (all channels loaded)
// TODO: try again in outer if fail to load a channel // TODO: try again in outer if fail to load a channel
execCommand('IS_READY', null, function (err, yes) { execCommand('IS_READY', null, function (err, yes) {
@ -784,6 +796,14 @@ define([
onPadChatReady(obj.data); onPadChatReady(obj.data);
return; return;
} }
if (obj.ev === 'DISCONNECT') {
onDisconnect();
return;
}
if (obj.ev === 'RECONNECT') {
onReconnect();
return;
}
}); });
}; };

Loading…
Cancel
Save