/** * SessionManager class to maintain the session on the client side and monitor if the network goes down * To activate the session manager with session extender, you need to call setExtenderUrl() with the path * to the JSON server that will handle the session extension. The JSON server must just return the following JSON: * { * "session_extended": "true" * } * Optionally, it can also return the current date and time in a field called "time". */ cambrient.SessionManager = function() { var session_extender_url = null; var self; // pointer to instance of this class var KEEP_ALIVE_PERIOD_MS = 1000*60*15; // keep session alive every fifteen minutes var SERVER_TIMEOUT_MS = 1000*60*29; // How long before the server communication is pronounced dead var lastServerSighting = 0; // last time we heard from the server function constructorFn() { self = this; } // Set the URL to the session extender JSON server (see module documentation above) constructorFn.prototype.setExtenderUrl = function(url) { session_extender_url = cambrient.contextPath + url; if (session_extender_url != null) setTimeout(self.keepAlive, KEEP_ALIVE_PERIOD_MS); } // Keep the session alive by making periodic requests to the server constructorFn.prototype.keepAlive = function() { if (session_extender_url == null) return; // the application needs to set this in order to activate the session manager var now = new Date(); if (lastServerSighting == 0) { lastServerSighting = new Date().getTime(); } if (now.getTime() - lastServerSighting > SERVER_TIMEOUT_MS) { // the connection to the server seems to have died. Warn the user cambrient.userMessaging.info('Have not heard from the server in ' + (SERVER_TIMEOUT_MS / (1000*60)) + ' minutes.'); } if (isFunction(session_extender_url.each)) { // array of session extender urls session_extender_url.each(function(url) { self.ajaxCall(url); }) } else { self.ajaxCall(session_extender_url); } setTimeout(self.keepAlive, KEEP_ALIVE_PERIOD_MS); } // The Ajax call to keep the session alive constructorFn.prototype.ajaxCall = function(url) { // We do not use the ajax module here because we do not want to use up a connection in it's pool new Ajax.Request( session_extender_url, { onSuccess: function(reply) { self.serverPing(reply); }, onFailure: function(reply) { cambrient.userMessaging.error('ERROR!! ' + reply); } } ); } // Callback for when the server replies constructorFn.prototype.serverPing = function(reply) { lastServerSighting = new Date().getTime(); } return new constructorFn(); } cambrient.sessionManager = new cambrient.SessionManager();