mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
feat(pusher): disconnect after 30m of inactivity
This commit is contained in:
@@ -4,16 +4,20 @@ angular.module('habitrpg')
|
||||
.factory('Pusher', ['$rootScope', 'STORAGE_SETTINGS_ID', 'Groups', 'Shared',
|
||||
function($rootScope, STORAGE_SETTINGS_ID, Groups, Shared) {
|
||||
var settings = JSON.parse(localStorage.getItem(STORAGE_SETTINGS_ID));
|
||||
var IS_PUSHER_ENABLED = window.env['PUSHER:ENABLED'] === 'true';
|
||||
var IS_PUSHER_ENABLED = true;
|
||||
|
||||
var partyId;
|
||||
var onActivityEvent;
|
||||
|
||||
var api = {
|
||||
pusher: undefined,
|
||||
socketId: undefined, // when defined the user is connected
|
||||
};
|
||||
var tabIdKey = 'habitica-active-tab';
|
||||
var tabId = Shared.uuid();
|
||||
|
||||
function connectToPusher (partyId) {
|
||||
localStorage.setItem(tabIdKey, Shared.uuid());
|
||||
localStorage.setItem(tabIdKey, tabId);
|
||||
window.onbeforeunload = function () {
|
||||
localStorage.removeItem(tabIdKey);
|
||||
}
|
||||
@@ -29,6 +33,22 @@ angular.module('habitrpg')
|
||||
},
|
||||
});
|
||||
|
||||
// Disconnect after 30m of inactivity
|
||||
var DISCONNECTION_AFTER = 5000; // 30m
|
||||
var disconnectionTimeout;
|
||||
|
||||
var awaitIdle = function() {
|
||||
if(disconnectionTimeout) clearTimeout(disconnectionTimeout);
|
||||
disconnectionTimeout = setTimeout(function () {
|
||||
console.log('Disconnecting from Pusher.');
|
||||
$(document).off('mousemove keydown mousedown touchstart', awaitIdle);
|
||||
disconnectPusher();
|
||||
}, DISCONNECTION_AFTER);
|
||||
};
|
||||
|
||||
awaitIdle();
|
||||
$(document).on('mousemove keydown mousedown touchstart', awaitIdle);
|
||||
|
||||
api.pusher.connection.bind('error', function(err) {
|
||||
console.error(err);
|
||||
// TODO if( err.data.code === 4004 ) detected connection limit
|
||||
@@ -84,6 +104,21 @@ angular.module('habitrpg')
|
||||
});
|
||||
};
|
||||
|
||||
function disconnectPusher () {
|
||||
api.pusher.disconnect();
|
||||
|
||||
var awaitActivity = function() {
|
||||
console.log('Reconnecting to Pusher.');
|
||||
$(document).off('mousemove keydown mousedown touchstart', awaitActivity);
|
||||
if (!localStorage.getItem(tabIdKey) || localStorage.getItem(tabIdKey) === tabId) {
|
||||
connectToPusher(partyId);
|
||||
}
|
||||
};
|
||||
|
||||
$(document).on('mousemove keydown mousedown touchstart', awaitActivity);
|
||||
|
||||
};
|
||||
|
||||
// Setup chat channels once app is ready, only for parties for now
|
||||
var clearAppLoadedListener = $rootScope.$watch('appLoaded', function (after) {
|
||||
if (!after) return;
|
||||
@@ -94,7 +129,7 @@ angular.module('habitrpg')
|
||||
var user = $rootScope.user;
|
||||
|
||||
// Connect the user to Pusher and to the party's chat channel
|
||||
var partyId = user && $rootScope.user.party && $rootScope.user.party._id;
|
||||
partyId = user && $rootScope.user.party && $rootScope.user.party._id;
|
||||
if (!partyId) return;
|
||||
|
||||
// See if another tab is already connected to Pusher
|
||||
|
||||
Reference in New Issue
Block a user