Skip to content

Instantly share code, notes, and snippets.

@jenyayel
Last active February 2, 2016 13:24
Show Gist options
  • Save jenyayel/d38174a1893d42168bc4 to your computer and use it in GitHub Desktop.
Save jenyayel/d38174a1893d42168bc4 to your computer and use it in GitHub Desktop.
Performs a simple requests for a given Uri on scheduled basis
<!DOCTYPE html>
<html>
<head>
<title>Pinger</title>
</head>
<body>
<a href="#">return back</a>
<hr />
<pre></pre>
<script src="pinger.js"></script>
</body>
</html>
(function () {
var _options = {
pingUrl: getQuery('pingUrl'),
frequency: parseInt(getQuery('frequency') || 10), // in seconds
returnUrl: getQuery('returnUrl') || document.referrer,
stopAfterFail: parseInt(getQuery('stopAfterFail') || 3), // after which failure to stop
strategy: getQuery('strategy') || 'xhr'
};
if (!validateInput(_options))
return;
document.getElementsByTagName('a')[0].onclick = () => {
window.location.href = _options.returnUrl;
return false;
};
var strategy = _options.strategy === 'xhr' ? xhrStrategy : iframeStrategy;
l(`Starting pinging with frequency ${_options.frequency} seconds using "${_options.strategy}" strategy...`);
var pending = false, failCount = 0;
var timer = setInterval(() => {
if (pending === true) {
l('Skipping scheduled ping since there is another one still pending');
return;
}
pending = true;
l('Sending request...');
strategy(_options.pingUrl, (result) => {
pending = false;
if (result.success) failCount = 0;
else failCount += 1;
l(`Request ${result.success ? 'done' : 'failed'}`, result.data);
if (failCount === _options.stopAfterFail) {
clearInterval(timer);
l(`Stopped pinging after ${failCount} failed attempts in a row.`);
}
});
}, _options.frequency * 1000);
function xhrStrategy(url, callback) {
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('GET', url);
xhr.onreadystatechange = () => {
if(xhr.readyState !== 4) return;
if (typeof callback === 'function'){
callback({
success: xhr.status === 200,
data: { status: xhr.status, body: xhr.responseText }
});
}
};
xhr.send(null);
}
function iframeStrategy(url, callback) {
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.onload = (event) => {
if (typeof callback === 'function') {
callback({
success: true,
data: event
});
}
iframe.remove();
};
iframe.setAttribute("src", url);
document.body.appendChild(iframe);
}
function validateInput(options)
{
if (!options.pingUrl) {
l('"pingUrl" is missing', options);
return false;
}
if (options.returnUrl === '') {
l('"returnUrl" or referrer is missing', options);
return false;
}
if (isNaN(options.frequency)) {
l('"frequency" must be a number', options);
return false;
}
if (isNaN(options.stopAfterFail)) {
l('"stopAfterFail" must be a number', options);
return false;
}
if (options.strategy !== 'iframe' && options.strategy !== 'xhr') {
l('"strategy" must be either "iframe" or "xhr"', options);
return false;
}
return true;
}
function l(message, params)
{
var _output = document.getElementsByTagName('pre')[0];
_output.innerHTML += message;
if (params)
_output.innerHTML += ' > ' + JSON.stringify(params);
_output.innerHTML += '<br>';
}
function getQuery(name) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == name)
return decodeURIComponent(pair[1]);
}
return false;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment