pusher: remove 1 tab limit, better disconnection

This commit is contained in:
Matteo Pagliazzi
2016-09-27 15:25:07 +02:00
parent 7bb2f4a3fa
commit faed0dff20
2 changed files with 16 additions and 63 deletions

View File

@@ -43,7 +43,7 @@
"smart-app-banner": "78ef9c0679723b25be1a0ae04f7b4aef7cbced4f", "smart-app-banner": "78ef9c0679723b25be1a0ae04f7b4aef7cbced4f",
"habitica-markdown": "1.2.2", "habitica-markdown": "1.2.2",
"pusher-js-auth": "^2.0.0", "pusher-js-auth": "^2.0.0",
"pusher-websocket-iso": "pusher#^3.1.0" "pusher-websocket-iso": "pusher#^3.2.0"
}, },
"devDependencies": { "devDependencies": {
"angular-mocks": "1.3.9" "angular-mocks": "1.3.9"

View File

@@ -7,39 +7,17 @@ angular.module('habitrpg')
var IS_PUSHER_ENABLED = window.env['PUSHER:ENABLED'] === 'true'; var IS_PUSHER_ENABLED = window.env['PUSHER:ENABLED'] === 'true';
var partyId; var partyId;
var partyChannel;
var onActivityEvent; var onActivityEvent;
var api = { var api = {
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-v1';
var tabId = Shared.uuid();
var localStorageDateInterval;
function onDisconnect () {
localStorage.removeItem(tabIdKey);
localStorage.removeItem(tabIdKey + '-time');
if (localStorageDateInterval) clearInterval(localStorageDateInterval);
}
function connectToPusher (partyId, reconnecting) { function connectToPusher (partyId, reconnecting) {
console.log('Connecting to Pusher.'); console.log('Connecting to Pusher.');
// Limit 1 tab connected per user
localStorage.setItem(tabIdKey, tabId);
localStorage.setItem(tabIdKey + '-time', Date.now());
// Every 10 seconds log the current time to localstorage, so if for some reason
// localStorage.tabIdKey get stuck, the next time the user reconnect and
// the last logged date has been long ago
localStorageDateInterval = setInterval(function () {
localStorage.setItem(tabIdKey + '-time', Date.now());
}, 10000);
window.onbeforeunload = onDisconnect;
api.pusher = new Pusher(window.env['PUSHER:KEY'], { api.pusher = new Pusher(window.env['PUSHER:KEY'], {
encrypted: true, encrypted: true,
authEndpoint: '/api/v3/user/auth/pusher', authEndpoint: '/api/v3/user/auth/pusher',
@@ -76,7 +54,7 @@ angular.module('habitrpg')
}); });
var partyChannelName = 'presence-group-' + partyId; var partyChannelName = 'presence-group-' + partyId;
var partyChannel = api.pusher.subscribe(partyChannelName); partyChannel = api.pusher.subscribe(partyChannelName);
// When an error occurs while joining the channel // When an error occurs while joining the channel
partyChannel.bind('pusher:subscription_error', function(status) { partyChannel.bind('pusher:subscription_error', function(status) {
@@ -153,12 +131,15 @@ angular.module('habitrpg')
Groups.data.party.chat.splice(200); Groups.data.party.chat.splice(200);
// If a system message comes in, sync the party as quest status may have changed // If a system message comes in, sync the party as quest status may have changed
// Sync once every 5 seconds, not more often
if (chatData.uuid === 'system') { if (chatData.uuid === 'system') {
_.throttle(function () {
$rootScope.User.sync(); $rootScope.User.sync();
Groups.party(true).then(function (syncedParty) { Groups.party(true).then(function (syncedParty) {
// Assign and not replace so that all the references get the modifications // Assign and not replace so that all the references get the modifications
_.assign($rootScope.party, syncedParty); _.assign($rootScope.party, syncedParty);
}); });
}, 5000);
} }
var docHasFocus = document.hasFocus(); var docHasFocus = document.hasFocus();
@@ -202,19 +183,13 @@ angular.module('habitrpg')
function disconnectPusher () { function disconnectPusher () {
console.log('Disconnecting from Pusher for inactivity.'); console.log('Disconnecting from Pusher for inactivity.');
partyChannel.unbind();
api.pusher.connection.unbind();
api.pusher.disconnect(); api.pusher.disconnect();
window.onbeforeunload = null; // TODO use addEventListener
// onDisconnect();
var awaitActivity = function() { var awaitActivity = function() {
$(document).off('mousemove keydown mousedown touchstart', awaitActivity); $(document).off('mousemove keydown mousedown touchstart', awaitActivity);
var lastSavedDate = localStorage.getItem(tabIdKey + '-time') || 0;
if (!localStorage.getItem(tabIdKey) || localStorage.getItem(tabIdKey) === tabId || ((Date.now() - lastSavedDate) > 30000)) {
connectToPusher(partyId, true); connectToPusher(partyId, true);
} else {
console.log('Cannot connect 2 tabs to Pusher.');
}
}; };
$(document).on('mousemove keydown mousedown touchstart', awaitActivity); $(document).on('mousemove keydown mousedown touchstart', awaitActivity);
@@ -230,32 +205,10 @@ angular.module('habitrpg')
var user = $rootScope.user; var user = $rootScope.user;
// 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 && user.party && user.party._id;
if (!partyId) return; if (!partyId) return;
// See if another tab is already connected to Pusher (or if it got stuck: last saved date more than 30 sec ago)
var lastSavedDate = localStorage.getItem(tabIdKey + '-time') || 0;
if (!localStorage.getItem(tabIdKey) || ((Date.now() - lastSavedDate) > 30000)) {
connectToPusher(partyId); connectToPusher(partyId);
} else {
console.log('Cannot connect 2 tabs to Pusher.');
}
// when a tab is closed, connect the next one
// wait between 100 and 500ms to avoid two tabs connecting at the same time
window.addEventListener('storage', function(e) {
if (e.key === tabIdKey && e.newValue === null) {
setTimeout(function () {
var lastSavedDate = localStorage.getItem(tabIdKey + '-time') || 0;
if (!localStorage.getItem(tabIdKey) || ((Date.now() - lastSavedDate) > 30000)) {
connectToPusher(partyId, true);
} else {
console.log('Cannot connect 2 tabs to Pusher.');
}
}, Math.floor(Math.random() * 501) + 100);
}
});
}); });
return api; return api;