How to determine latency of a remote server through the browser

后端 未结 9 721
暖寄归人
暖寄归人 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:06

    Before the call to the server, record the Javascript time:

    var startTime = new Date();
    

    Load an image from the server:

    var img = new Image()
    img.onload = function() {
        // record end time
    }
    img.src = "http://server1.domain.com/ping.jpg";
    

    As soon as the request is finished, record the time again. (Given of course that the request didn't time out.)

    var endTime = new Date();
    

    Your ping in milliseconds is:

    var ping = endTime. getTime() - startTime.getTime();
    

提交回复
热议问题