3

I'm basically following the accepted answer to this question (Is it possible to ping a server from Javascript?)

Update

It seems to work as expected when the domain is 15 characters long (actually, http:// + 15, but 16 or more causes it to bomb. More details at the bottom.


The issue I'm seeing is that if you're using something that seems like a valid domain, for example http://thisisdefinitelynotarealdomainname.com, it returns an error but the code mentioned considers errors okay (because most should be). Looking at the error event, I'm not sure I see where I could get the HTTP response code (i.e., if it's a 404, consider it invalid).

Here is a jsFiddle showing the problem -- they all display "responded". If you look in the console, the invalid domain returns a 404 error, and the two valid ones (if in chrome console, not sure about the others) show that they were interpreted as an image but transferred as text/html -- is there any way to read either the 404 error, or the mime type?

var pinger = function () {
    var ping = function (ip, callback) {
        if (!this.inUse) {
            this.status = 'unchecked';
            this.inUse = true;
            this.callback = callback;
            this.ip = ip;
            var _that = this;
            this.img = new Image();
            this.img.onload = function () {
                _that.inUse = false;
                _that.callback('responded');
            };
            this.img.onerror = function (e) {
                if (_that.inUse) {
                    _that.inUse = false;
                    _that.callback('responded', e);
                }
                console.log(e);
            };
            this.start = new Date().getTime();
            this.img.src = "http://" + ip + "/?now=" + this.start; // add the current time to work around caching
            this.time = setTimeout(function () {
                if (_that.inUse) {
                    _that.inUse = false;
                    _that.callback('timeout');
                }
            }, 1500);
        }
    }

    return {
        ping: ping
    };
}();

(function () {
    var output = document.getElementById('output');
    var servers = [
        'localhost',
        'google.com',
        'okthisreallydoesntmakeanysense',
        'okthisreallydoe',
        'thisisashortone',
        'thisisabitlonger'
    ];

    servers.forEach(function (server) {
        new pinger.ping(server, function (status, e) {
            output.innerHTML += server + ': ' + status + '<br />';
        });
    });
})();    

Update

What's even more weird is that it seems to be fine up until 15 characters. I've updated the jsFiddle. See below on ones that respond how I'd expect vs ones that don't. What might cause this?

        'localhost',
        'google.com',
        'okthisreallydoesntmakeanysense', // doesn't work
        'okthisreallydoe', // works (15 characters)
        'thisisashortone', // works (15 characters)
        'thisisabitlonger' // doesn't work (16 characters)
Community
  • 1
  • 1
Tom
  • 2,032
  • 6
  • 27
  • 44
  • 2
    If the domain is invalid then there is not going to be an http server to give you a 404 is there? – Deadron Feb 04 '14 at 16:25
  • 1
    My understanding of a callback is not to print back 'responded' but to call a function `responded(Event e)`.. the script is somehow unreliable IMHO. – Daniel W. Feb 04 '14 at 16:26
  • @DanFromGermany the callback is `function(status, e) [..]` and it's being used like `callback('responded', e)` – Tom Feb 04 '14 at 16:30
  • @Deadron yeah, maybe not.. 502 or something. Whatever the case, I'm just wondering how I could read what the error is – Tom Feb 04 '14 at 16:30
  • If you hit an invalid domain in chrome it responds with a 204. I dont know exactly what is responding though or if its supplied by the browser... – Deadron Feb 04 '14 at 17:33
  • @Tom An HTTP status code comes from an HTTP server. As Deadron pointed out, if the name doesn't resolve then you have no network address to even try to connect to. Without that, you're certainly not going to get connected to an HTTP server, and you're definitely not getting **any** HTTP status code back because you can't connect and make an HTTP request in the first place. – Brad Mar 16 '14 at 06:16

1 Answers1

0

This might help.

function Pinger_ping(ip, callback) {

  if(!this.inUse) {

    this.inUse = true;
    this.callback = callback
    this.ip = ip;

    var _that = this;

    this.img = new Image();

    this.img.onload = function() {_that.good();};
    this.img.onerror = function() {_that.good();};

    this.start = new Date().getTime();
    this.img.src = "http://" + ip;
    this.timer = setTimeout(function() { _that.bad();}, 1500);

  }
}

Let me know if it works

Lpc_dark
  • 2,466
  • 7
  • 26
  • 45