Skip to content

Commit

Permalink
#310 keeping-session-opened even if moderator leaves (Disconnect+Rejo…
Browse files Browse the repository at this point in the history
…in.html)

connection.peers.backup is renamed as “connection.peersBackup”. (used
internally)

Skipping duplicate “onstream” events.

Improved TranlsationHandler.js. Now it works on any domain and any
site. The “connection.googKey” is now open & free!

#310 It currently works only for 2-3 levels. It is a known bug. It
works if top moderator leaves, and if next moderator leaves. But
doesn’t works for upcoming (3rd or 4th) moderators.
  • Loading branch information
muaz-khan committed Jan 2, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8d58721 commit 3138852
Showing 9 changed files with 141 additions and 80 deletions.
21 changes: 21 additions & 0 deletions Signaling-Server.js
Original file line number Diff line number Diff line change
@@ -61,6 +61,9 @@ module.exports = exports = function(app, socketCallback) {
var params = socket.handshake.query;
var socketMessageEvent = params.msgEvent || 'RTCMultiConnection-Message';

var sessionid = params.sessionid;
var autoCloseEntireSession = params.autoCloseEntireSession;

if (params.enableScalableBroadcast) {
if (!ScalableBroadcast) {
ScalableBroadcast = require('./Scalable-Broadcast.js');
@@ -80,6 +83,14 @@ module.exports = exports = function(app, socketCallback) {
socket.userid = params.userid;
appendUser(socket);

if (autoCloseEntireSession == 'false' && sessionid == socket.userid) {
socket.shiftModerationControlBeforeLeaving = true;
}

socket.on('shift-moderator-control-on-disconnect', function() {
socket.shiftModerationControlBeforeLeaving = true;
});

socket.on('extra-data-updated', function(extra) {
try {
if (!listOfUsers[socket.userid]) return;
@@ -438,14 +449,24 @@ module.exports = exports = function(app, socketCallback) {
try {
// inform all connected users
if (listOfUsers[socket.userid]) {
var firstUserSocket = null;

for (var s in listOfUsers[socket.userid].connectedWith) {
if (!firstUserSocket) {
firstUserSocket = listOfUsers[socket.userid].connectedWith[s];
}

listOfUsers[socket.userid].connectedWith[s].emit('user-disconnected', socket.userid);

if (listOfUsers[s] && listOfUsers[s].connectedWith[socket.userid]) {
delete listOfUsers[s].connectedWith[socket.userid];
listOfUsers[s].socket.emit('user-disconnected', socket.userid);
}
}

if (socket.shiftModerationControlBeforeLeaving && firstUserSocket) {
firstUserSocket.emit('become-next-modrator', sessionid);
}
}
} catch (e) {
pushLogs('disconnect', e);
2 changes: 2 additions & 0 deletions demos/Disconnect+Rejoin.html
Original file line number Diff line number Diff line change
@@ -159,6 +159,8 @@ <h1>Disconnect+Rejoin using RTCMultiConnection</h1>

var connection = new RTCMultiConnection();

connection.autoCloseEntireSession = false;

// by default, socket.io server is assumed to be deployed on your own URL
connection.socketURL = '/';

3 changes: 3 additions & 0 deletions dev/DetectRTC.js
Original file line number Diff line number Diff line change
@@ -189,6 +189,7 @@
interval = 10,
isTimeout = false;
var id = window.setInterval(

function() {
if (isDone()) {
window.clearInterval(id);
@@ -242,6 +243,7 @@

if (typeof isPrivate === 'undefined') {
retry(

function isDone() {
return db.readyState === 'done' ? true : false;
},
@@ -275,6 +277,7 @@
}

retry(

function isDone() {
return typeof isPrivate !== 'undefined' ? true : false;
},
4 changes: 2 additions & 2 deletions dev/MultiPeersHandler.js
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@
function MultiPeers(connection) {
var self = this;

var skipPeers = ['getAllParticipants', 'getLength', 'selectFirst', 'streams', 'send', 'forEach', 'backup'];
var skipPeers = ['getAllParticipants', 'getLength', 'selectFirst', 'streams', 'send', 'forEach'];
connection.peersBackup = {};
connection.peers = {
backup: {},
getLength: function() {
var numberOfPeers = 0;
for (var peer in this) {
18 changes: 12 additions & 6 deletions dev/RTCMultiConnection.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,13 @@ function RTCMultiConnection(roomid, forceOptions) {

var mPeer = new MultiPeers(connection);

var preventDuplicateOnStreamEvents = {};
mPeer.onGettingLocalMedia = function(stream) {
if (preventDuplicateOnStreamEvents[stream.streamid]) {
return;
}
preventDuplicateOnStreamEvents[stream.streamid] = true;

stream.type = 'local';

connection.setStreamEndHandler(stream);
@@ -88,8 +94,8 @@ function RTCMultiConnection(roomid, forceOptions) {
};
}

if (connection.peers.backup[streamEvent.userid]) {
streamEvent.extra = connection.peers.backup[streamEvent.userid].extra;
if (connection.peersBackup[streamEvent.userid]) {
streamEvent.extra = connection.peersBackup[streamEvent.userid].extra;
}

connection.onstreamended(streamEvent);
@@ -263,8 +269,8 @@ function RTCMultiConnection(roomid, forceOptions) {
extra: connection.peers[remoteUserId] ? connection.peers[remoteUserId].extra : {}
};

if (connection.peers.backup[eventObject.userid]) {
eventObject.extra = connection.peers.backup[eventObject.userid].extra;
if (connection.peersBackup[eventObject.userid]) {
eventObject.extra = connection.peersBackup[eventObject.userid].extra;
}

connection.onleave(eventObject);
@@ -1143,8 +1149,8 @@ function RTCMultiConnection(roomid, forceOptions) {
return;
}

if (connection.peers.backup[streamEvent.userid]) {
streamEvent.extra = connection.peers.backup[streamEvent.userid].extra;
if (connection.peersBackup[streamEvent.userid]) {
streamEvent.extra = connection.peersBackup[streamEvent.userid].extra;
}

connection.onstreamended(streamEvent);
24 changes: 17 additions & 7 deletions dev/SocketConnection.js
Original file line number Diff line number Diff line change
@@ -2,8 +2,10 @@ function SocketConnection(connection, connectCallback) {
var parameters = '';

parameters += '?userid=' + connection.userid;
parameters += '&sessionid=' + connection.sessionid;
parameters += '&msgEvent=' + connection.socketMessageEvent;
parameters += '&socketCustomEvent=' + connection.socketCustomEvent;
parameters += '&autoCloseEntireSession=' + !!connection.autoCloseEntireSession;

parameters += '&maxParticipantsAllowed=' + connection.maxParticipantsAllowed;

@@ -57,14 +59,14 @@ function SocketConnection(connection, connectCallback) {
extra: extra
});

if (!connection.peers.backup[remoteUserId]) {
connection.peers.backup[remoteUserId] = {
if (!connection.peersBackup[remoteUserId]) {
connection.peersBackup[remoteUserId] = {
userid: remoteUserId,
extra: {}
};
}

connection.peers.backup[remoteUserId].extra = extra;
connection.peersBackup[remoteUserId].extra = extra;
});

connection.socket.on(connection.socketMessageEvent, function(message) {
@@ -87,8 +89,8 @@ function SocketConnection(connection, connectCallback) {
var action = message.message.action;

if (action === 'ended' || action === 'inactive' || action === 'stream-removed') {
if (connection.peers.backup[stream.userid]) {
stream.extra = connection.peers.backup[stream.userid].extra;
if (connection.peersBackup[stream.userid]) {
stream.extra = connection.peersBackup[stream.userid].extra;
}
connection.onstreamended(stream);
return;
@@ -246,8 +248,8 @@ function SocketConnection(connection, connectCallback) {
extra: {}
};

if (connection.peers.backup[eventObject.userid]) {
eventObject.extra = connection.peers.backup[eventObject.userid].extra;
if (connection.peersBackup[eventObject.userid]) {
eventObject.extra = connection.peersBackup[eventObject.userid].extra;
}

connection.onleave(eventObject);
@@ -350,4 +352,12 @@ function SocketConnection(connection, connectCallback) {
connection.socket.on('room-full', function(roomid) {
connection.onRoomFull(roomid);
});

connection.socket.on('become-next-modrator', function(sessionid) {
if (sessionid != connection.sessionid) return;
setTimeout(function() {
connection.open(sessionid);
connection.socket.emit('shift-moderator-control-on-disconnect');
}, 1000);
});
}
44 changes: 22 additions & 22 deletions dev/TranslationHandler.js
Original file line number Diff line number Diff line change
@@ -41,32 +41,32 @@ var TranslationHandler = (function() {
document.getElementsByTagName('head')[0].appendChild(newScript);
},
getListOfLanguages: function(callback) {
var newScript = document.createElement('script');
newScript.type = 'text/javascript';

var randomNumber = 'method' + connection.token();
window[randomNumber] = function(response) {
if (response.data && response.data.languages && callback) {
callback(response.data.languages);
return;
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
var response = JSON.parse(xhr.responseText);

if (response.error && response.error.message === 'Daily Limit Exceeded') {
console.error('Text translation failed. Error message: "Daily Limit Exceeded."');
return;
}
if (response && response.data && response.data.languages) {
callback(response.data.languages);
return;
}

if (response.error) {
console.error(response.error.message);
return;
}
if (response.error && response.error.message === 'Daily Limit Exceeded') {
console.error('Text translation failed. Error message: "Daily Limit Exceeded."');
return;
}

console.error(response);
};
if (response.error) {
console.error(response.error.message);
return;
}

var source = 'https://www.googleapis.com/language/translate/v2/languages?key=' + connection.googKey;
newScript.src = source;
document.getElementsByTagName('head')[0].appendChild(newScript);
console.error(response);
}
}
var url = 'https://www.googleapis.com/language/translate/v2/languages?key=' + connection.googKey + '&target=en';
xhr.open('GET', url, true);
xhr.send(null);
}
};
}
Loading

0 comments on commit 3138852

Please sign in to comment.