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

pull/1/head
Yann Flory 9 years ago
parent c576241104
commit b1e8bcddef

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

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

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

Loading…
Cancel
Save