Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

JavaScript: Retry on HTTP GET Connection Timeout

Hi there,

I need to add the feature of if the website is unavailable, to wait 10 seconds and try again. If my website is still unavailable, try again 5 times, if still navailable, echo "UnableToConnect" then exit.

IT WOULD LOGICALLY LOOK SOMETHING LIKE THIS (but in Java Script):

SET ConnectCounter=0
:Home
Echo http://www.MyWebSite.com
Send the GET command to the website and ECHO it out
If can't connect to http://www.MyWebSite.com  (
   If %ConnectCounter% GEQ 5 (
   ECHO UnableToConnect
   EXIT
   )
  SET /a ConnectCounter=%ConnectCounter%+1
  Add a delay of 10Seconds
)
Goto home


HERE IS MY ORIGINAL WORKING JAVA SCRIPT:
var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);
notyetready = 0;
}
}

var objArgs = WScript.Arguments;
WScript.Echo("http://www.MyWebSite.com");
request.open( "GET", "http://www.MyWebSite.com" , true );
request.send(null);

while( notyetready )
{
WScript.Sleep( 100 );
}



Thanks for your help,
Rene
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Avatar of ReneGe

ASKER

leakim971,

Thanks for your reference.

I tried to modify my code with yours without success.

Could you please help me with this?

This script is to monitor my website and I used Wikipedia website for the purpose of this post.

Thanks,
Rene


COMMAND LINE FROM MY BATCH FILE TO LAUNCH THE JAVA SCRIPT:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia


JAVA SCRIPT MODIFIED AS PER leakim971's LINK :
var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);
notyetready = 0;
}
}

      var counter = 0;
      function timer() {
                var objArgs = WScript.Arguments;
                WScript.Echo(objArgs(0));
            request.open( "GET", objArgs(0) , true );
                request.send(null);
            setTimeout("timer()", 1000); // try to reload the picture every 1 seconds.
      }
      function resetCounter() {
            counter = 0;
      }
      function increaseCounter() {
            counter++;
            // If we fail to reload the pic 5 times, display alert message
            if(counter>5) alert("Your browser appears to be offline.\nPlease check your internet connection and try again.");
            
      }


while( notyetready )
{
WScript.Sleep( 100 );
}


Why replace ? Use the provided code entirely ? Or what does it miss?
Avatar of ReneGe

ASKER

I tried it but it did not work.
I need to call your script with a batch file, adding as an argument, the website to be monitored, and have the web site dumped in a text file for analysis.

FOR EXAMPLE:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt

Have any ideas?

Thanks,
Rene
>cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt

Not with javascript : You cannot run javasript from command line.
You need a VBScript
Avatar of ReneGe

ASKER

Hmmmmm

When I call the following JavaScript from a cammand line, it works.

Am I missing something here?


COMMAND LINE: cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt


JAVA SCRIPT (NAME: Monitor.js):

var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);
notyetready = 0;
}
}

var objArgs = WScript.Arguments;
WScript.Echo(objArgs(0));
request.open( "GET", objArgs(0) , true );
request.send(null);

while( notyetready )
{
WScript.Sleep( 100 );
}
WScript is VBScript not javascript. You can run VBScript from command line (with or without cscript) but not javascript.
Avatar of ReneGe

ASKER

Ok, now I am confised. The script I provided is a WScript, and that is also called VBScript?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

leakim971,

I posted it as WScript and got it done.

Thanks for your help,
Rene