Z
zborisau
Hey good people,
I've been given a problem to solve recently - and stuck with the
solution for a good 4 days already.
i have a link which leads to popup window. the purpose of that popup
window is to redirect user to the one of available servers.
so i need to check which server is available. i have decided to
download some 1pixel image from every server and if it was downloaded
then the server is available. the other thing i have to keep in mind is
that 3 servers have some order of preference, ie server1 is prefered to
have user redirected to then server2 and then server3.
the way i do it similar to the one below:
#########################################################
<img id="scheck0" style="display:none;">
<img id="scheck1" style="display:none;">
<img id="scheck2" style="display:none;">
<div id="progress">Connecting to server...</div>
<script>
function showError(link)
{
document.getElementById("progress").innerHTML=link;
}
function redirect(link)
{
window.location=link;
}
var serverCheckTimeoutMillis =10000 //10 seconds
var isServer0=false;
var isServer1=false;
var isServer2=false;
var image0 = document.getElementById("scheck0");
image0.src = "www.server0.com/image1px.gif";
image0.onload = function() {
isServer0=true;
};
image0.onabort = function() {
showError("failed to connect");
};
var image1 = document.getElementById("scheck1");
image1.src = "www.server1.com/image1px.gif";
image1.onload = function() {
isServer1=true;
};
image1.onabort = function() {
showError("failed to connect");
};
var image2 = document.getElementById("scheck2");
image2.src = "www.server2.com/image1px.gif";
image2.onload = function() {
isServer2=true;
};
image2.onabort = function() {
showError("failed to connect");
};
var dNow = new Date();
var startTimeMillis = dNow.getTime();
var intervalID = window.setInterval("checkServers()",500);
function checkServers()
{
var isTimeoutHappened = false;
var currentTimeMillis = -1;
dNow = new Date();
currentTimeMillis = dNow.getTime();
if ((currentTimeMillis - startTimeMillis) >=
serverCheckTimeoutMillis)
isTimeoutHappened = true;
if (isServer0 == true)
{
redirect("www.server0.com/form.html");
window.clearInterval(intervalID);
}
else if (isServer0 == false && isServer1 == true &&
isTimeoutHappened)
{
redirect("www.server1.com/form.html);
window.clearInterval(intervalID);
}
else if (isServer1 == false && isServer2 == true &&
isTimeoutHappened)
{
redirect("www.server2.com/form.html");
window.clearInterval(intervalID);
}
else if (isServer0 == false && isServer1 == false && isServer2 ==
false && isTimeoutHappened)
{
window.clearInterval(intervalID);
showError("failed to connect");
}
}
</script>
#########################################################
the code is quite simple:
1. load three images simultaneously
2. onload handler of every image will change isServer0(or1 or2) to true
once image from particular server is loaded.
3. every half a minute checkServer() function is called (it was set
using window.setInterval("checkServers()",500); interval ) and checks
which server is available and redirects user to the correct server.
4. checkServer() always checks for the isTimeoutHappened boolean var -
which is set to true if serverCheckTimeoutMillis milliseconds has
passed from the start of script execution.
the reason for timeout is simple.
by default browser waits somewhere around 30 seconds before it decides
to stop some image download should this image location be unavailable.
the isTimeoutHappened will be set to true after 10 seconds of script
execution - and on this occasion i decide that server is not available.
I just do not wait for those 30 seconds
AND NOW PROBLEM:
firefox works beautiful with this code.
internet explorer (i am using ver 6) does not work under one case:
server0 - is offline
server1 - is offline
server2 - is online
IE tries to download all three images and whilst first two cannot be
downloaded, it downloads third one no problem, but the onload event for
the 3rd image is never fired unless the first two images has
timeout-ed.
So under IE isTimeoutHappened happends before server2 image onload
event has fired.
I checked this suggestion through this code:
<img src="www.server0.com/image1px.gif">
<img src="www.server1.com/image1px.gif">
<img src="www.server2.com/image1px.gif">
and stupid IE will not show third image (even though it has downloaded
it already) unless the download for the first two has timed out.
When I checked this stuff in firefox - i noticed that firefox displays
images as it downloads them - thus onload events get fired timedly.
To correct this problem i believe that I have to cancel image loading
after some timeout time (in my cae it is 10 seconds).
So far I have not got any ideas how to do that.
I tried to fire my own abort events to those images - no chance.
Browser does not gicve a damn about it.
If you have any solution for such a problem, or may be u have another
solution on how to pick up the first available server (in some order of
priority) and redirect user to that server (using javascript strictly)
- I will gladly appreciate all comments.
Best regards,
Boris
I've been given a problem to solve recently - and stuck with the
solution for a good 4 days already.
i have a link which leads to popup window. the purpose of that popup
window is to redirect user to the one of available servers.
so i need to check which server is available. i have decided to
download some 1pixel image from every server and if it was downloaded
then the server is available. the other thing i have to keep in mind is
that 3 servers have some order of preference, ie server1 is prefered to
have user redirected to then server2 and then server3.
the way i do it similar to the one below:
#########################################################
<img id="scheck0" style="display:none;">
<img id="scheck1" style="display:none;">
<img id="scheck2" style="display:none;">
<div id="progress">Connecting to server...</div>
<script>
function showError(link)
{
document.getElementById("progress").innerHTML=link;
}
function redirect(link)
{
window.location=link;
}
var serverCheckTimeoutMillis =10000 //10 seconds
var isServer0=false;
var isServer1=false;
var isServer2=false;
var image0 = document.getElementById("scheck0");
image0.src = "www.server0.com/image1px.gif";
image0.onload = function() {
isServer0=true;
};
image0.onabort = function() {
showError("failed to connect");
};
var image1 = document.getElementById("scheck1");
image1.src = "www.server1.com/image1px.gif";
image1.onload = function() {
isServer1=true;
};
image1.onabort = function() {
showError("failed to connect");
};
var image2 = document.getElementById("scheck2");
image2.src = "www.server2.com/image1px.gif";
image2.onload = function() {
isServer2=true;
};
image2.onabort = function() {
showError("failed to connect");
};
var dNow = new Date();
var startTimeMillis = dNow.getTime();
var intervalID = window.setInterval("checkServers()",500);
function checkServers()
{
var isTimeoutHappened = false;
var currentTimeMillis = -1;
dNow = new Date();
currentTimeMillis = dNow.getTime();
if ((currentTimeMillis - startTimeMillis) >=
serverCheckTimeoutMillis)
isTimeoutHappened = true;
if (isServer0 == true)
{
redirect("www.server0.com/form.html");
window.clearInterval(intervalID);
}
else if (isServer0 == false && isServer1 == true &&
isTimeoutHappened)
{
redirect("www.server1.com/form.html);
window.clearInterval(intervalID);
}
else if (isServer1 == false && isServer2 == true &&
isTimeoutHappened)
{
redirect("www.server2.com/form.html");
window.clearInterval(intervalID);
}
else if (isServer0 == false && isServer1 == false && isServer2 ==
false && isTimeoutHappened)
{
window.clearInterval(intervalID);
showError("failed to connect");
}
}
</script>
#########################################################
the code is quite simple:
1. load three images simultaneously
2. onload handler of every image will change isServer0(or1 or2) to true
once image from particular server is loaded.
3. every half a minute checkServer() function is called (it was set
using window.setInterval("checkServers()",500); interval ) and checks
which server is available and redirects user to the correct server.
4. checkServer() always checks for the isTimeoutHappened boolean var -
which is set to true if serverCheckTimeoutMillis milliseconds has
passed from the start of script execution.
the reason for timeout is simple.
by default browser waits somewhere around 30 seconds before it decides
to stop some image download should this image location be unavailable.
the isTimeoutHappened will be set to true after 10 seconds of script
execution - and on this occasion i decide that server is not available.
I just do not wait for those 30 seconds
AND NOW PROBLEM:
firefox works beautiful with this code.
internet explorer (i am using ver 6) does not work under one case:
server0 - is offline
server1 - is offline
server2 - is online
IE tries to download all three images and whilst first two cannot be
downloaded, it downloads third one no problem, but the onload event for
the 3rd image is never fired unless the first two images has
timeout-ed.
So under IE isTimeoutHappened happends before server2 image onload
event has fired.
I checked this suggestion through this code:
<img src="www.server0.com/image1px.gif">
<img src="www.server1.com/image1px.gif">
<img src="www.server2.com/image1px.gif">
and stupid IE will not show third image (even though it has downloaded
it already) unless the download for the first two has timed out.
When I checked this stuff in firefox - i noticed that firefox displays
images as it downloads them - thus onload events get fired timedly.
To correct this problem i believe that I have to cancel image loading
after some timeout time (in my cae it is 10 seconds).
So far I have not got any ideas how to do that.
I tried to fire my own abort events to those images - no chance.
Browser does not gicve a damn about it.
If you have any solution for such a problem, or may be u have another
solution on how to pick up the first available server (in some order of
priority) and redirect user to that server (using javascript strictly)
- I will gladly appreciate all comments.
Best regards,
Boris