4

I'm playing around with implementing a JavaScript server ping tool based on the accepted answer given on this question: Is it possible to ping a server from Javascript?. This essentially works by assuming the pinged server is down if no response has been given after n milliseconds.

That's great, and it's a pretty cool way of doing it, however there are two rather large pitfalls:

  1. Not all servers do respond within the allocated time.
  2. Sometimes an ERR_CONNECTION_TIMED_OUT error is thrown before our timeout timer has finished.

Both of these things cause incorrect results. The former suggests that the server is offline when it's possibly online and responding slowly, and the latter suggests the server is online when it's (likely) offline.

In an ideal world this code would capture what type of error thrown was thrown and handle this appropriately. After all, if the error thrown is a 404 Not Found error, this counter-intuitively means the server is online and has responded.

If we log the image error event, the only thing we see surrounding the error is:

Event {
  ...
  type: "error"
}

There's no message or anything hinting at what the error thrown was, and both the 404 and ERR_CONNECTION_TIMED_OUT errors give identical information.

What can I do to capture the ERR_CONNECTION_TIMED_OUT error I see in Chrome's JavaScript console, rather than relying on a fixed-speed timer?


Update

The best way I can replicate this issue is by altering Trante's JSFiddle demo (as linked to in the question I've linked above) to use a 30000ms timer rather than a 1500ms timer:

this.timer = setTimeout(function () {
    if (_that.inUse) {
        _that.inUse = false;
        _that.callback('timeout');
    }
}, 30000);

The 'unknown' server should obviously not respond, but instead we see this:

Example

In Chrome's console, the following error has been thrown:

Failed to load resource: net::ERR_NAME_NOT_RESOLVED

As the Image's onerror function has been fired with the generic error as given above, the function believes this to mean that 1. 'unknown' exists, and 2. it's online. The ERR_NAME_NOT_RESOLVED error appears to be something which only Chrome is aware of, and isn't passed through to the error event at all.


Update 2

Today I tried doing this with web sockets instead of images and unfortunately these suffer from the same problem. The only data surrounding the error returned is type: "error" - no information about what the error actually was.

Community
  • 1
  • 1
James Donnelly
  • 117,312
  • 30
  • 193
  • 198
  • I'm guessing the downvote and lack of comment or close vote means it was just random and not given for any particular reason, so thanks for that... – James Donnelly Feb 27 '15 at 23:12
  • Interesting question (and I don't know the answer) but isn't asking if a server is online and just responding too slow kind of like the old saw about a tree falling in the woods and no one being around to hear it? What does it matter if its online if its not responding within what you determined is an adequate time? – George Mauer Feb 27 '15 at 23:26
  • @GeorgeMauer the first situation isn't that relevant, you're right, if a server fails to respond in a given time I'm happy to assume it's down. The second bullet point is where the main problem lies though; if my browser thinks the connection has timed out before my timer does, my code just assumes the server must be online because a generic "error" object has been generated. Unfortunately this appears to happen at random times. I've had it happen after just 3 seconds, but I've also *not* had it happen after more than a minute. Setting the timer to just 3 seconds doesn't seem long enough. – James Donnelly Feb 27 '15 at 23:32
  • odd, when I use the method mentioned I only get 200 status codes in chrome network tab (though obviously an error). Can you recreate condition 2 reliably? – George Mauer Feb 27 '15 at 23:47

0 Answers0