How to determine latency of a remote server through the browser

后端 未结 9 722
暖寄归人
暖寄归人 2020-12-17 15:41

I run a couple of game tunnelling servers and would like to have a page where the client can run a ping on all the servers and find out which is the most responsive. As far

相关标签:
9条回答
  • 2020-12-17 16:24

    If you are talking about running something client side, I am not sure this is possible due to security reasons.

    Maybe your best bet would be a java applet - but again this needs to be checked against local security policy.

    If I try to think about some hack in JS to do this, maybe you can try to send an async request with a callback function which measures the milliseconds it took - but this is just off the top of my head.

    0 讨论(0)
  • 2020-12-17 16:26

    Here's an <iframe> approach:


    (source: magnetiq.com)

    Create a table (not necessarily in the literal <table> sense) with two columns. The first column will hold the name of servers (and possibly links to them). The second column has iframes that load probe documents from the respective servers. Each probe document does this on the initial fetch request:

    1. Get current system time
    2. Do a redirect (302) to a second probe document while passing the system time as a query parameter
    3. The second probe document reads the current system time, calculates the delta from the initial reading that was passed to it and just displays it in big fat letters. This delta will be the time it took for the server to respond to the client with a redirect response plus the time it took for the client to make the second request to the redirection target. It's not exactly a "ping" but it's a comparable measure of the client's relative latency with each server. In fact, it's a "reverse ping" from the server to the client.

    You'd be using iframes without infringing the same-domain policy because there's no attempt at manipulating the iframe contents at all. The player will simply see the values with his/her own eyes and you'll rely on the user glancing at the numbers and clicking on the server link that makes the most sense.

    0 讨论(0)
  • 2020-12-17 16:28

    All you really need is the time from the connection start, to the time of the first readystate change...

    function getPing() {
      var start;
      var client = getClient(); // xmlhttprequest object
      client.onreadystatechange = function() {
        if (client.readyState > 0) {
          pingDone(start); //handle ping
          client.onreadystatechange = null; //remove handler
        } 
      }
    
      start = new Date();
      client.open("HEAD", "/ping.txt"); //static file
      client.send();
    }
    
    function pingDone(start) {
      done = new Date();
      ms = done.valueOf() - start.valueOf();
      alert(ms + "ms ping time");
    }
    
    function getClient() {
      if (window.XMLHttpRequest)
        return new XMLHttpRequest();
    
      if (window.ActiveXObject)
        return new ActiveXObject('MSXML2.XMLHTTP.3.0');
    
      throw("No XMLHttpRequest Object Available.");
    }
    
    0 讨论(0)
提交回复
热议问题