Is it possible to ping a server from Javascript?

后端 未结 17 922
礼貌的吻别
礼貌的吻别 2020-11-22 04:02

I\'m making a web app that requires that I check to see if remote servers are online or not. When I run it from the command line, my page load goes up to a full 60s (for 8 e

相关标签:
17条回答
  • 2020-11-22 04:35

    I have found someone that accomplishes this with a very clever usage of the native Image object.

    From their source, this is the main function (it has dependences on other parts of the source but you get the idea).

    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);
    
      }
    }
    

    This works on all types of servers that I've tested (web servers, ftp servers, and game servers). It also works with ports. If anyone encounters a use case that fails, please post in the comments and I will update my answer.

    Update: Previous link has been removed. If anyone finds or implements the above, please comment and I'll add it into the answer.

    Update 2: @trante was nice enough to provide a jsFiddle.

    http://jsfiddle.net/GSSCD/203/

    Update 3: @Jonathon created a GitHub repo with the implementation.

    https://github.com/jdfreder/pingjs

    Update 4: It looks as if this implementation is no longer reliable. People are also reporting that Chrome no longer supports it all, throwing a net::ERR_NAME_NOT_RESOLVED error. If someone can verify an alternate solution I will put that as the accepted answer.

    0 讨论(0)
  • 2020-11-22 04:36

    The problem with standard pings is they're ICMP, which a lot of places don't let through for security and traffic reasons. That might explain the failure.

    Ruby prior to 1.9 had a TCP-based ping.rb, which will run with Ruby 1.9+. All you have to do is copy it from the 1.8.7 installation to somewhere else. I just confirmed that it would run by pinging my home router.

    0 讨论(0)
  • 2020-11-22 04:40

    You can't do regular ping in browser Javascript, but you can find out if remote server is alive by for example loading an image from the remote server. If loading fails -> server down.

    You can even calculate the loading time by using onload-event. Here's an example how to use onload event.

    0 讨论(0)
  • 2020-11-22 04:40

    Pitching in with a websocket solution...

    function ping(ip, isUp, isDown) {
      var ws = new WebSocket("ws://" + ip);
      ws.onerror = function(e){
        isUp();
        ws = null;
      };
      setTimeout(function() { 
        if(ws != null) {
          ws.close();
          ws = null;
          isDown();
        }
      },2000);
    }
    
    0 讨论(0)
  • 2020-11-22 04:40

    There are many crazy answers here and especially about CORS -

    You could do an http HEAD request (like GET but without payload). See https://ochronus.com/http-head-request-good-uses/

    It does NOT need a preflight check, the confusion is because of an old version of the specification, see Why does a cross-origin HEAD request need a preflight check?

    So you could use the answer above which is using the jQuery library (didn't say it) but with

    type: 'HEAD'
    

    --->

    <script>
        function ping(){
           $.ajax({
              url: 'ping.html',
              type: 'HEAD',
              success: function(result){
                 alert('reply');
              },     
              error: function(result){
                  alert('timeout/error');
              }
           });
        }
    </script>
    

    Off course you can also use vanilla js or dojo or whatever ...

    0 讨论(0)
  • 2020-11-22 04:43

    You can run the DOS ping.exe command from javaScript using the folowing:

    function ping(ip)
    {
        var input = "";
        var WshShell = new ActiveXObject("WScript.Shell");
        var oExec = WshShell.Exec("c:/windows/system32/ping.exe " + ip);
    
        while (!oExec.StdOut.AtEndOfStream)
        {
                input += oExec.StdOut.ReadLine() + "<br />";
        }
        return input;
    }
    

    Is this what was asked for, or am i missing something?

    0 讨论(0)
提交回复
热议问题