disable limit of 1 tab per browser connected to pusher

This commit is contained in:
Matteo Pagliazzi
2016-09-07 16:50:22 +02:00
parent 861eaf36d3
commit dafae31366

View File

@@ -13,14 +13,16 @@ angular.module('habitrpg')
pusher: undefined, pusher: undefined,
socketId: undefined, // when defined the user is connected socketId: undefined, // when defined the user is connected
}; };
var tabIdKey = 'habitica-active-tab'; // Limit of 1 connected tab is disabled for now
var tabId = Shared.uuid(); // var tabIdKey = 'habitica-active-tab';
// var tabId = Shared.uuid();
function connectToPusher (partyId) { function connectToPusher (partyId) {
localStorage.setItem(tabIdKey, tabId); // Limit of 1 connected tab is disabled for now
window.onbeforeunload = function () { // localStorage.setItem(tabIdKey, tabId);
localStorage.removeItem(tabIdKey); // window.onbeforeunload = function () {
} // localStorage.removeItem(tabIdKey);
// }
api.pusher = new Pusher(window.env['PUSHER:KEY'], { api.pusher = new Pusher(window.env['PUSHER:KEY'], {
encrypted: true, encrypted: true,
@@ -40,7 +42,6 @@ angular.module('habitrpg')
var awaitIdle = function() { var awaitIdle = function() {
if(disconnectionTimeout) clearTimeout(disconnectionTimeout); if(disconnectionTimeout) clearTimeout(disconnectionTimeout);
disconnectionTimeout = setTimeout(function () { disconnectionTimeout = setTimeout(function () {
console.log('Disconnecting from Pusher.');
$(document).off('mousemove keydown mousedown touchstart', awaitIdle); $(document).off('mousemove keydown mousedown touchstart', awaitIdle);
disconnectPusher(); disconnectPusher();
}, DISCONNECTION_AFTER); }, DISCONNECTION_AFTER);
@@ -110,15 +111,14 @@ angular.module('habitrpg')
api.pusher.disconnect(); api.pusher.disconnect();
var awaitActivity = function() { var awaitActivity = function() {
console.log('Reconnecting to Pusher.');
$(document).off('mousemove keydown mousedown touchstart', awaitActivity); $(document).off('mousemove keydown mousedown touchstart', awaitActivity);
if (!localStorage.getItem(tabIdKey) || localStorage.getItem(tabIdKey) === tabId) { // Limit of 1 connected tab is disabled for now
connectToPusher(partyId); // if (!localStorage.getItem(tabIdKey) || localStorage.getItem(tabIdKey) === tabId) {
} // connectToPusher(partyId);
// }
}; };
$(document).on('mousemove keydown mousedown touchstart', awaitActivity); $(document).on('mousemove keydown mousedown touchstart', awaitActivity);
}; };
// Setup chat channels once app is ready, only for parties for now // Setup chat channels once app is ready, only for parties for now
@@ -132,24 +132,27 @@ angular.module('habitrpg')
// Connect the user to Pusher and to the party's chat channel // Connect the user to Pusher and to the party's chat channel
partyId = user && $rootScope.user.party && $rootScope.user.party._id; partyId = user && $rootScope.user.party && $rootScope.user.party._id;
// if (!partyId) return; if (!partyId) return;
// See if another tab is already connected to Pusher
if (!localStorage.getItem(tabIdKey)) {
connectToPusher(partyId); connectToPusher(partyId);
}
// DISABLED FOR NOW
// See if another tab is already connected to Pusher
// if (!localStorage.getItem(tabIdKey)) {
// connectToPusher(partyId);
// }
// when a tab is closed, connect the next one // when a tab is closed, connect the next one
// wait between 100 and 500ms to avoid two tabs connecting at the same time // wait between 100 and 500ms to avoid two tabs connecting at the same time
window.addEventListener('storage', function(e) { // window.addEventListener('storage', function(e) {
if (e.key === tabIdKey && e.newValue === null) { // if (e.key === tabIdKey && e.newValue === null) {
setTimeout(function () { // setTimeout(function () {
if (!localStorage.getItem(tabIdKey)) { // if (!localStorage.getItem(tabIdKey)) {
connectToPusher(partyId); // connectToPusher(partyId);
} // }
}, Math.floor(Math.random() * 501) + 100); // }, Math.floor(Math.random() * 501) + 100);
} // }
}); // });
}); });
return api; return api;