remove limit of 1 connected tab per browser

This commit is contained in:
Matteo Pagliazzi
2016-09-07 16:44:20 +02:00
parent a4ecdbeb3e
commit 0baff2dfd9

View File

@@ -13,14 +13,17 @@ 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';
var tabId = Shared.uuid(); // Limit of 1 connected tab is disabled for now
// var tabIdKey = 'habitica-active-tab';
// var tabId = Shared.uuid();
function connectToPusher (partyId, reconnecting) { function connectToPusher (partyId, reconnecting) {
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 +43,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);
@@ -130,11 +132,8 @@ 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) { connectToPusher(partyId, true);
connectToPusher(partyId, true);
}
}; };
$(document).on('mousemove keydown mousedown touchstart', awaitActivity); $(document).on('mousemove keydown mousedown touchstart', awaitActivity);
@@ -153,22 +152,23 @@ angular.module('habitrpg')
partyId = user && $rootScope.user.party && $rootScope.user.party._id; partyId = user && $rootScope.user.party && $rootScope.user.party._id;
if (!partyId) return; if (!partyId) return;
// DISABLED FOR NOW
// See if another tab is already connected to Pusher // See if another tab is already connected to Pusher
if (!localStorage.getItem(tabIdKey)) { // if (!localStorage.getItem(tabIdKey)) {
connectToPusher(partyId); // 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, true); // connectToPusher(partyId, true);
} // }
}, Math.floor(Math.random() * 501) + 100); // }, Math.floor(Math.random() * 501) + 100);
} // }
}); // });
}); });
return api; return api;