javascript 如何使用Javascript检测最近的服务器以测试延迟/ping速度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17027193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:49:54  来源:igfitidea点击:

How to detect nearest server to test latency/ping speed using Javascript?

javascriptpinglatencygeoipspeed-test

提问by Ashraf Bashir

I'm implementing a browser extension which should connect to the nearest server to test the ping speed.

To have more details, kindly have a look on SpeedTest, it first calculates the ping time.
And to calculate this ping time, it detects the nearest servers and then pings it.

For example, here's the nearest servers detected by SpeedTest. I marked them in red arrows

我正在实现一个浏览器扩展,它应该连接到最近的服务器来测试 ping 速度。

要了解更多详细信息,请查看SpeedTest,它首先计算 ping 时间。
为了计算这个 ping 时间,它会检测最近的服务器,然后 ping 它。

例如,这里是SpeedTest检测到的最近服务器。我用红色箭头标记了它们

SpeedTest detected nearest servers

SpeedTest 检测到最近的服务器


How does SpeedTestdetect these servers ?

i.e. How can I detect the nearest servers and their locations in javascript as SpeedTestdoes ?


SpeedTest如何检测这些服务器?

即如何像SpeedTest那样在 javascript 中检测最近的服务器及其位置?

回答by Ben McCormick

What speedtest is doing is not a real "ping" in the sense of having the client machine run a ping operation. I have no knowledge of their exact methods (and I believe they use flash rather than pure JS, or at least used to), but I would guess that they try to access a small resource (an empty text/image file?) on each of these machines, and then time the response back. That is certainly one way to implement this anyway.

在让客户端机器运行 ping 操作的意义上,speedtest 所做的并不是真正的“ping”。我不知道他们的确切方法(我相信他们使用 flash 而不是纯 JS,或者至少曾经使用过),但我猜他们试图访问每个这些机器,然后时间响应回来。无论如何,这当然是实现这一点的一种方法。