Air XmlHttpRequest timeout if remote server is down?
I am writing an AIR application that communicates with a server via XmlHttpRequest.
The problem I am facing is that if the server is not available, my asynchronous XmlHttpRequest will never fire. My onreadystatechange handler detects an OPEN state, but nothing else.
Is there a way to disable the XmlHttpRequest?
Do I need to do something stupid like use setTimeout () to wait for a while and then abort () if no connection is established?
Edit: Found this one , but in my testing, wrapping my xmlhttprequest.send () in a try / catch block or setting a value in xmlhttprequest.timeout (or TimeOut or timeOut) doesnβt have any effect.
source to share
With AIR, as with XHR elsewhere, you must set a timer in JavaScript to detect connection timeouts.
var xhReq = createXMLHttpRequest();
xhReq.open("get", "infiniteLoop.phtml", true); // Server stuck in a loop.
var requestTimer = setTimeout(function() {
xhReq.abort();
// Handle timeout situation, e.g. Retry or inform user.
}, MAXIMUM_WAITING_TIME);
xhReq.onreadystatechange = function() {
if (xhReq.readyState != 4) { return; }
clearTimeout(requestTimer);
if (xhReq.status != 200) {
// Handle error, e.g. Display error message on page
return;
}
var serverResponse = xhReq.responseText;
};
source to share
XMLHttpRequest timeout and ontimeout are synchronous and must be implemented in js client with callbacks :
Example:
function isUrlAvailable(callback, error) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
return callback();
}
else {
setTimeout(function () {
return error();
}, 8000);
}
};
xhttp.open('GET', siteAddress, true);
xhttp.send();
}
source to share