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);
};
var emit = function (ev, data) {
eachHandler('event', function (f) {
f(ev, data);
});
};
messenger.on = function (type, f) {
var stack = messenger.handlers[type];
if (!Array.isArray(stack)) {
@ -575,21 +581,16 @@ define([
var openChannel = function (data) {
var keys = data.keys;
var encryptor = data.encryptor || Curve.createEncryptor(keys);
network.join(data.channel).then(function (chan) {
var channel = channels[data.channel] = {
var channel = {
id: data.channel,
isFriendChat: data.isFriendChat,
isPadChat: data.isPadChat,
sending: false,
encryptor: encryptor,
messages: [],
wc: chan,
userList: [],
mapId: {},
};
chan.on('message', function (msg, sender) {
onMessage(msg, sender, chan);
});
var onJoining = function (peer) {
if (peer === Msg.hk) { return; }
@ -599,7 +600,7 @@ define([
// 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 msg = [Types.mapId, myData, channel.wc.myID];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
var data = {
@ -608,13 +609,8 @@ define([
};
network.sendto(peer, JSON.stringify(data));
};
chan.members.forEach(function (peer) {
if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer);
});
chan.on('join', onJoining);
chan.on('leave', function (peer) {
var onLeaving = function (peer) {
var i = channel.userList.indexOf(peer);
while (i !== -1) {
channel.userList.splice(i, 1);
@ -634,13 +630,36 @@ define([
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) {
onMessage(msg, sender, chan);
});
chan.members.forEach(function (peer) {
if (peer === Msg.hk) { return; }
if (channel.userList.indexOf(peer) !== -1) { return; }
channel.userList.push(peer);
});
chan.on('join', onJoining);
chan.on('leave', onLeaving);
// FIXME don't subscribe to the channel implicitly
getChannelMessagesSince(channel, data, keys);
}, function (err) {
};
network.join(data.channel).then(onOpen, function (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) {
@ -805,9 +824,7 @@ define([
// TODO load rooms
}).nThen(function () {
ready = true;
eachHandler('event', function (f) {
f('READY');
});
emit('READY');
});
};
init();
@ -883,9 +900,7 @@ define([
var openPadChat = function (data, cb) {
var channel = data.channel;
if (getChannel(channel)) {
eachHandler('event', function (f) {
f('PADCHAT_READY', channel);
});
emit('PADCHAT_READY', channel);
return void cb();
}
var keys = data.secret && data.secret.keys;
@ -901,13 +916,18 @@ define([
};
openChannel(chanData);
joining[channel] = function () {
eachHandler('event', function (f) {
f('PADCHAT_READY', channel);
});
emit('PADCHAT_READY', channel);
};
cb();
};
network.on('disconnect', function () {
emit('DISCONNECT');
});
network.on('reconnect', function () {
emit('RECONNECT');
});
messenger.execCommand = function (obj, cb) {
var cmd = obj.cmd;
var data = obj.data;

@ -122,6 +122,12 @@ define([
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 channels = Object.keys(state.channels).sort(function (a, b) {
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
var w = $userlist[0].offsetWidth - $userlist[0].clientWidth;
$userlist.css('width', (68 + w)+'px');
onResize();
};
$(window).on('resize', function () {
var w = $userlist[0].offsetWidth - $userlist[0].clientWidth;
$userlist.css('width', (68 + w)+'px');
});
$(window).on('resize', onResize);
var m = function (md) {
var d = h('div.cp-app-contacts-content');
@ -174,7 +176,8 @@ define([
markup.message = function (msg) {
if (msg.type !== 'MSG') { return; }
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 day = d ? d.toLocaleDateString() : '';
var hour = d ? d.toLocaleTimeString() : '';
@ -770,6 +773,15 @@ define([
$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)
// TODO: try again in outer if fail to load a channel
execCommand('IS_READY', null, function (err, yes) {
@ -784,6 +796,14 @@ define([
onPadChatReady(obj.data);
return;
}
if (obj.ev === 'DISCONNECT') {
onDisconnect();
return;
}
if (obj.ev === 'RECONNECT') {
onReconnect();
return;
}
});
};

Loading…
Cancel
Save