Replace the Netflux old client (netflux.js) by the Netflux2 client.

Move the WebRTC peer-to-peer use case in /padrtc, which still uses the old
Netflux client
Use es6-promises.min.js to solve a issue with some browser and the new
Netflux client
This commit is contained in:
Yann Flory
2016-04-07 18:48:01 +02:00
parent cf9f60bd57
commit 0b3d6e15b8
14 changed files with 2628 additions and 160 deletions

View File

@@ -17,10 +17,11 @@
window.Reflect = { has: (x,y) => { return (y in x); } };
define([
'/common/messages.js',
'/common/netflux.js',
'/common/netflux-client.js',
'/common/crypto.js',
'/common/toolbar.js',
'/_socket/text-patcher.js',
'/common/es6-promise.min.js',
'/common/chainpad.js',
'/bower_components/jquery/dist/jquery.min.js',
], function (Messages, Netflux, Crypto, Toolbar, TextPatcher) {
@@ -75,7 +76,6 @@ define([
function (config)
{
var websocketUrl = config.websocketURL;
var webrtcUrl = config.webrtcURL;
var userName = config.userName;
var channel = config.channel;
var chanKey = config.cryptKey;
@@ -122,25 +122,20 @@ define([
content.length + ':' + content;
};
var onPeerMessage = function(toId, type, wc) {
if(type === 6) {
messagesHistory.forEach(function(msg) {
wc.sendTo(toId, '1:y'+msg);
});
wc.sendTo(toId, '0');
}
};
var whoami = new RegExp(userName.replace(/[\/\+]/g, function (c) {
return '\\' +c;
}));
var onMessage = function(peer, msg, wc) {
var onMessage = function(peer, msg, wc, network) {
if(msg === 0 || msg === '0') {
onReady(wc);
var hc = (wc && wc.history_keeper) ? wc.history_keeper : null;
if(wc && (msg === 0 || msg === '0')) {
onReady(wc, network);
return;
}
else if (peer === hc){
msg = JSON.parse(msg)[4];
}
var message = chainpadAdapter.msgIn(peer, msg);
verbose(message);
@@ -176,8 +171,10 @@ define([
users: []
};
var onJoining = function(peer) {
if(peer.length !== 32) { return; }
var list = userList.users;
if(list.indexOf(peer) === -1) {
var index = list.indexOf(peer);
if(index === -1) {
userList.users.push(peer);
}
userList.onChange();
@@ -216,7 +213,7 @@ define([
if(parsed.content[0] === 4) { // PING message from Chainpad
parsed.content[0] = 5;
onMessage('', '1:y'+mkMessage(parsed.user, parsed.channelId, parsed.content));
wc.sendPing();
// wc.sendPing();
return;
}
return Crypto.encrypt(msg, cryptKey);
@@ -227,20 +224,6 @@ define([
key: ''
};
var rtc = true;
if(!getParameterByName("webrtc") || !webrtcUrl) {
rtc = false;
options.signaling = websocketUrl;
options.topology = 'StarTopologyService';
options.protocol = 'WebSocketProtocolService';
options.connector = 'WebSocketService';
options.openWebChannel = true;
}
else {
options.signaling = webrtcUrl;
}
var createRealtime = function(chan) {
return ChainPad.create(userName,
passwd,
@@ -251,12 +234,12 @@ define([
});
};
var onReady = function(wc) {
var onReady = function(wc, network) {
if(config.onInit) {
config.onInit({
myID: wc.myID,
realtime: realtime,
webChannel: wc,
getLag: network.getLag,
userList: userList
});
}
@@ -274,18 +257,21 @@ define([
}
}
var onOpen = function(wc) {
var onOpen = function(wc, network) {
channel = wc.id;
window.location.hash = channel + '|' + chanKey;
// Add the existing peers in the userList
wc.members.forEach(onJoining);
// Add the handlers to the WebChannel
wc.onmessage = function(peer, msg) { // On receiving message
onMessage(peer, msg, wc);
};
wc.onJoining = onJoining; // On user joining the session
wc.onLeaving = onLeaving; // On user leaving the session
wc.onPeerMessage = function(peerId, type) {
onPeerMessage(peerId, type, wc);
};
wc.on('message', function (msg, sender) { //Channel msg
onMessage(sender, msg, wc, network);
});
wc.on('join', onJoining);
wc.on('leave', onLeaving);
if(config.setMyID) {
config.setMyID({
myID: wc.myID
@@ -299,7 +285,7 @@ define([
// Filter messages sent by Chainpad to make it compatible with Netflux
message = chainpadAdapter.msgOut(message, wc);
if(message) {
wc.send(message).then(function() {
wc.bcast(message).then(function() {
// Send the message back to Chainpad once it is sent to the recipients.
onMessage(wc.myID, message);
}, function(err) {
@@ -311,17 +297,11 @@ define([
// Get the channel history
var hc;
if(rtc) {
wc.channels.forEach(function (c) { if(!hc) { hc = c; } });
if(hc) {
wc.getHistory(hc.peerID);
}
}
else {
// TODO : Improve WebSocket service to use the latest Netflux's API
wc.peers.forEach(function (p) { if (!hc || p.linkQuality > hc.linkQuality) { hc = p; } });
hc.send(JSON.stringify(['GET_HISTORY', wc.id]));
}
wc.members.forEach(function (p) {
if (p.length === 16) { hc = p; }
});
wc.history_keeper = hc;
if (hc) { network.sendto(hc, JSON.stringify(['GET_HISTORY', wc.id])); }
toReturn.patchText = TextPatcher.create({
@@ -331,58 +311,30 @@ define([
realtime.start();
};
var createRTCChannel = function () {
// Check if the WebRTC channel exists and create it if necessary
var webchannel = Netflux.create();
webchannel.openForJoining(options).then(function(data) {
onOpen(webchannel);
onReady(webchannel);
}, function(error) {
warn(error);
});
};
var findChannelById = function(webChannels, channelId) {
var webChannel;
webChannels.forEach(function(chan) {
if(chan.id == channelId) { webChannel = chan; return;}
});
return webChannel;
}
var joinChannel = function() {
// Connect to the WebSocket/WebRTC channel
Netflux.join(channel, options).then(function(wc) {
onOpen(wc);
}, function(error) {
if(rtc && error.code === 1008) {// Unexisting RTC channel
createRTCChannel();
// Connect to the WebSocket channel
Netflux.connect(websocketUrl).then(function(network) {
network.on('message', function (msg, sender) { // Direct message
var wchan = findChannelById(network.webChannels, channel);
if(wchan) {
onMessage(sender, msg, wchan, network);
}
else { warn(error); }
});
};
joinChannel();
var checkConnection = function(wc) {
if(wc.channels && wc.channels.size > 0) {
var channels = Array.from(wc.channels);
var channel = channels[0];
var socketChecker = setInterval(function () {
if (channel.checkSocket(realtime)) {
warn("Socket disconnected!");
recoverableErrorCount += 1;
if (recoverableErrorCount >= MAX_RECOVERABLE_ERRORS) {
warn("Giving up!");
realtime.abort();
try { channel.close(); } catch (e) { warn(e); }
if (config.onAbort) {
config.onAbort({
socket: channel
});
}
if (socketChecker) { clearInterval(socketChecker); }
}
} else {
// it's working as expected, continue
}
}, 200);
}
};
network.join(channel || null).then(function(wc) {
onOpen(wc, network);
}, function(error) {
console.error(error);
})
}, function(error) {
warn(error);
});
return toReturn;
};