Fix the user list not working with the WebRTC service and causing major issues

This commit is contained in:
Yann Flory 2016-03-11 17:09:27 +01:00
parent c576241104
commit b1e8bcddef
3 changed files with 73 additions and 64 deletions

View File

@ -9,7 +9,6 @@ var run = module.exports.run = function(server) {
socket.on('message', (data) => {
try {
let msg = JSON.parse(data)
console.log(msg);
if (msg.hasOwnProperty('key')) {
for (let master of server.clients) {
if (master.key === msg.key) {

View File

@ -129,7 +129,7 @@ return /******/ (function(modules) { // webpackBootstrap
webChannel.onopen = function () {
resolve(webChannel);
};
});
}, reject);
});
}
}, {
@ -222,7 +222,9 @@ return /******/ (function(modules) { // webpackBootstrap
_createClass(WebChannel, [{
key: 'leave',
value: function leave() {}
value: function leave() {
this.topologyService.leave(this);
}
}, {
key: 'send',
value: function send(data) {
@ -246,12 +248,11 @@ return /******/ (function(modules) { // webpackBootstrap
}
}, {
key: 'sendTo',
value: function sendTo(id, msg) {
value: function sendTo(fromId, toId, msg) {
var channel = this;
return new Promise(function (resolve, reject) {
var protocol = _ServiceProvider2.default.get(channel.settings.protocol);
console.log('WCsendTo ' + id);
channel.topologyService.sendTo(id, channel, protocol.message(cs.USER_DATA, { id: channel.myID, data: msg })).then(resolve, reject);
channel.topologyService.sendTo(toId, channel, protocol.message(cs.USER_DATA, { id: fromId, data: msg })).then(resolve, reject);
});
}
}, {
@ -405,7 +406,7 @@ return /******/ (function(modules) { // webpackBootstrap
value: function get(code) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var service = undefined;
var service = void 0;
switch (code) {
case cs.WEBRTC_SERVICE:
service = new _WebRTCService2.default(options);
@ -467,8 +468,6 @@ return /******/ (function(modules) { // webpackBootstrap
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, FullyConnectedService);
console.log('SERVICE FULLY CONNECTED CONSTRUCTED');
}
_createClass(FullyConnectedService, [{
@ -594,7 +593,8 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'leave',
value: function leave(webChannel) {
this.broadcast(webChannel);
var protocol = _ServiceProvider2.default.get(cs.EXCHANGEPROTOCOL_SERVICE);
this.broadcast(webChannel, protocol.message(cs.LEAVING, { id: webChannel.myID }));
}
}, {
key: '_generateID',
@ -846,7 +846,7 @@ return /******/ (function(modules) { // webpackBootstrap
var settings = Object.assign({}, this.settings, options);
return new Promise(function (resolve, reject) {
var connection = undefined;
var connection = void 0;
var socket = new window.WebSocket(settings.signaling);
socket.onopen = function () {
connection = new _this2.RTCPeerConnection(settings.webRTCOptions);
@ -869,6 +869,9 @@ return /******/ (function(modules) { // webpackBootstrap
}, reject);
}, reject);
};
socket.onclose = function (e) {
reject(e);
};
socket.onmessage = function (e) {
var msg = JSON.parse(e.data);
if (Reflect.has(msg, 'data')) {
@ -963,8 +966,8 @@ return /******/ (function(modules) { // webpackBootstrap
}, function () {});
})();
} else if (msg.sdp.type === 'answer') {
var sd = Object.assign(new this.RTCSessionDescription(), msg.sdp);
webChannel.connections.get(msg.senderPeerID).setRemoteDescription(sd, function () {}, function () {});
var _sd = Object.assign(new this.RTCSessionDescription(), msg.sdp);
webChannel.connections.get(msg.senderPeerID).setRemoteDescription(_sd, function () {}, function () {});
}
} else if (Reflect.has(msg, 'candidate')) {
webChannel.connections.get(msg.senderPeerID).addIceCandidate(new this.RTCIceCandidate(msg.candidate));
@ -1077,7 +1080,7 @@ return /******/ (function(modules) { // webpackBootstrap
var settings = Object.assign({}, this.settings, options);
return new Promise(function (resolve, reject) {
var connection = undefined;
var connection = void 0;
var socket = new window.WebSocket(settings.signaling);
socket.seq = 1;
socket.facade = options.facade || null;
@ -1146,6 +1149,9 @@ return /******/ (function(modules) { // webpackBootstrap
case cs.GET_HISTORY:
webChannel.onPeerMessage(msg.id, msg.code);
break;
case cs.LEAVING:
webChannel.onLeaving(msg.id);
break;
case cs.SERVICE_DATA:
var service = _ServiceProvider2.default.get(msg.service);
service.onmessage(channel, msg.data);
@ -1180,6 +1186,9 @@ return /******/ (function(modules) { // webpackBootstrap
case cs.GET_HISTORY:
msg.id = data.id;
break;
case cs.LEAVING:
msg.id = data.id;
break;
case cs.SERVICE_DATA:
msg.service = data.service;
msg.data = Object.assign({}, data.data);
@ -1312,7 +1321,7 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'message',
value: function message(code, data) {
var type = undefined;
var type = void 0;
switch (code) {
case cs.USER_DATA:
type = 'MSG';

View File

@ -125,7 +125,6 @@ define([
var chainpadAdapter = {
usernamesMapping : {},
msgIn : function(peerId, msg) {
console.log('RECU : '+ msg);
var parsed = parseMessage(msg);
if(parsed.content[0] === 0) {
if(peerId) {
@ -142,30 +141,37 @@ define([
} catch (err) {
return message;
}
},
msgOut : function(msg) {
console.log('ENVOI : '+ msg);
var parsed = parseMessage(msg);
if(parsed.content[0] === 0) { // Someone is registering
onMessage('', '1:y'+mkMessage('', channel, [1,0]));
onMessage('', '1:y'+mkMessage('', channel, [3,0]));
return msg;
}
if(parsed.content[0] === 4) { // Someone is registering
console.log('ping');
console.log(parsed);
parsed.content[0] = 5;
onMessage('', '1:y'+mkMessage(parsed.user, parsed.channelId, parsed.content));
return;
}
return Crypto.encrypt(msg, cryptKey);
}
},
leaving : function(peerId) {
if(this.usernamesMapping[peerId]) {
var chainpadUser = this.usernamesMapping[peerId]
onMessage('', '1:y'+mkMessage(chainpadUser, channel, [3,0]));
}
if(this.usernamesMapping[peerId]) {
var chainpadUser = this.usernamesMapping[peerId]
onMessage('', '1:y'+mkMessage(chainpadUser, channel, [3,0]));
}
},
historyOut : function(msg) {
var parsed = parseMessage(msg);
if(parsed.content[0] === 0) {
var value = parsed.user;
var obj = this.usernamesMapping;
// Find the key (peerId) associated to the chainpad user in the map
var peerId = Object.keys(obj).filter(function(key) {return obj[key] === value})[0];
return peerId;
}
return;
}
};
@ -174,10 +180,9 @@ define([
};
var rtc = true;
var connected = false;
var realtime;
if(!getParameterByName("webrtc")) {
if(!getParameterByName("webrtc") || !webrtcUrl) {
rtc = false;
options.signaling = websocketUrl;
options.topology = 'StarTopologyService';
@ -208,6 +213,10 @@ define([
onPeerMessage(peerId, type, wc);
}
window.onunload = function() {
wc.leave();
}
// Open a Chainpad session
realtime = createRealtime();
@ -232,7 +241,7 @@ define([
if(message) {
wc.send(message).then(function() {
// Send the message back to Chainpad once it is sent to all peers if using the WebRTC protocol
if(rtc) { onMessage('', message); }
if(rtc) { onMessage(wc.myID, message); }
});
}
});
@ -263,52 +272,47 @@ define([
bump = realtime.bumpSharejs;
realtime.start();
// window.setInterval(function() {
// console.log(realtime.getLag());
// }, 1000);
};
if(rtc) {
// Check if the WebRTC channel exists and create it if necessary
var webchannel = Netflux.create();
webchannel.openForJoining(options).then(function(data) {
connected = true;
onOpen(webchannel);
}, function(error) {
warn(error);
});
}
if(!connected) {
// Connect to the WebSocket/WebRTC channel
Netflux.join(channel, options).then(function(wc) {
connected = true;
onOpen(wc);
}, function(error) {
warn(error);
});
}
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);
}, function(error) {
warn(error);
});
};
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();
}
else { warn(error); }
});
};
joinChannel();
var whoami = new RegExp(userName.replace(/[\/\+]/g, function (c) {
return '\\' +c;
}));
var onPeerMessage = function(peerID, type, wc) {
var onPeerMessage = function(toId, type, wc) {
if(type === 6) {
messagesHistory.forEach(function(msg) {
console.log(msg);
wc.sendTo(peerID, msg);
var fromId = chainpadAdapter.historyOut('1:y'+msg) || wc.myID;
wc.sendTo(fromId, toId, '1:y'+msg);
});
}
};
var onMessage = function(peer, msg) {
// remove the password
message = chainpadAdapter.msgIn(peer, msg);
verbose(message);
@ -318,7 +322,6 @@ define([
onEvent();
}
}
console.log(message);
realtime.message(message);
if (/\[5,/.test(message)) { verbose("pong"); }
@ -336,13 +339,11 @@ define([
}
}
var onJoining = function(peer, channel) {
console.log('Someone joined : '+peer)
var onJoining = function(peer) {
}
var onLeaving = function(peer, channel) {
var onLeaving = function(peer) {
chainpadAdapter.leaving(peer);
console.log('Someone left : '+peer)
}
var checkConnection = function(wc) {