Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Issue #77: Improve keepalive script
Browse files Browse the repository at this point in the history
  • Loading branch information
johanjanssens committed May 3, 2016
1 parent b5aabf2 commit 1973903
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions code/template/helper/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function validator($config = array())
/**
* Keep session alive
*
* This will send an ascynchronous request to the server via AJAX on an interval in miliseconds
* This will send an ascynchronous request to the server via AJAX on an interval in secs
*
* @param array $config An optional array with configuration options
* @return string The html output
Expand All @@ -283,8 +283,8 @@ public function keepalive($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
'refresh' => 15 * 60000, //default refresh is 15min
'url' => '', //default to window.location.url
'refresh' => 15 * 60, //default refresh is 15min
'url' => '', //default to window.location.url
));

$html = '';
Expand All @@ -296,16 +296,16 @@ public function keepalive($config = array())
if($session->isActive())
{
//Get the config session lifetime
$lifetime = $session->getLifetime() * 1000;
$lifetime = $session->getLifetime();

//Refresh time is 1 minute less than the lifetime
$refresh = ($lifetime <= 60000) ? 30000 : $lifetime - 60000;
$refresh = ($lifetime <= 60) ? 30 : $lifetime - 60;
}
else $refresh = (int) $config->refresh;

// Longest refresh period is one hour to prevent integer overflow.
if ($refresh > 3600000 || $refresh <= 0) {
$refresh = 3600000;
if ($refresh > 3600 || $refresh <= 0) {
$refresh = 3600;
}

if(empty($config->url)) {
Expand All @@ -315,22 +315,18 @@ public function keepalive($config = array())
}

// Build the keep alive script.
//See: http:https://stackoverflow.com/questions/5052543/how-to-fire-ajax-request-periodically
$html =
"<script>
(function keepalive(){
kQuery(function($) {
(function($){
var refresh = '" . $refresh . "';
setInterval(function() {
$.ajax({
url: $url,
method: 'HEAD',
cache: false,
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(keepalive, '" . $refresh . "');
}
cache: false
})
});
})();</script>";
}, refresh * 1000);
})(kQuery);</script>";

self::$_loaded['keepalive'] = true;
}
Expand Down

0 comments on commit 1973903

Please sign in to comment.