如何通过浏览器确定远程服务器的延迟 [英] How to determine latency of a remote server through the browser

查看:28
本文介绍了如何通过浏览器确定远程服务器的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行了几个游戏隧道服务器,并希望有一个页面,客户端可以在其中对所有服务器运行 ping 并找出响应最快的服务器.据我所知,在 JavaScript 中似乎没有正确的方法可以做到这一点,但我在想,有人知道在 Flash 或其他一些客户端浏览器技术中可以做到这一点的方法吗?

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 as I can see there seems to be no proper way to do this in JavaScript, but I was thinking, does anybody know of a way to do this in flash or some other client browser technology maybe?

推荐答案

大多数小程序技术,包括 Javascript,都强制执行同源策略.可以使用 onload 事件处理程序动态添加 DOM 元素(例如图像)并收集时间信息.

Most applet technology, including Javascript, enforces a same-origin policy. It may be possible to dynamically add DOM elements, such as images, and collect timing information using the onload event handler.

伪代码

for (server in servers) {
  var img = document.createElement('IMG');
  server.startTime = getCurrentTimeInMS();
  img.onload=function() { server.endTime = getcurrentTimeInMS(); }
  img.src = server.imgUrl;
}

然后等待适当的时间并检查每个服务器对象的时间.根据需要重复并计算平均值.我不确定您可以期待什么样的准确度.

Then wait an appropriate time and check the timing for each server object. Repeat as needed and compute averages if you want. I'm not sure what kind of accuracy you can expect.

缺点:

  • 您可能使用了错误的工具来完成这项工作.浏览器未配备此类应用程序.
  • 这可能很不准确.
  • 如果您请求的资源被缓存,它不会给您想要的结果,但您可以通过每次更改 url 来解决这个问题.
  • 与普通 ping 相比,这需要大量带宽.使图像变小,例如spacer.gif 文件.
  • 时间不仅取决于远程服务器的延迟,还取决于该服务器的带宽.这可能是一种或多或少有用的衡量标准,但重要的是要注意,这不仅仅是延迟.
  • 您需要能够处理来自不同服务器的 HTTP 请求,而且至关重要的是,每个服务器都应该提供完全相同的资源(或相同长度的资源).服务器上的条件会影响响应时间,例如一台服务器是否正在压缩数据,而另一台则不是.

这篇关于如何通过浏览器确定远程服务器的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆