如何使用 JavaScript ping IP 地址
我想运行 JavaScript 代码来 ping 4 个不同的 IP 地址,然后检索这些 ping 请求的丢包和延迟并将其显示在页面上。
我该怎么做?
I want to run a JavaScript code to ping 4 different IP addresses and then retrieve the packet loss and latency of these ping requests and display them on the page.
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你不能从 JS 做到这一点。您可以做的是:
向您的服务器发出 AJAX 请求,然后服务器将为您 ping 目标服务器,并在 AJAX 结果中返回结果。
可能的注意事项:
You can't do this from JS. What you could do is this:
Make an AJAX request to your server, which will then ping the target servers for you, and return the result in the AJAX result.
Possible caveats:
我能想到的唯一方法是从外部服务器加载图像文件。当负载失败时,您“知道”服务器没有响应(您实际上不知道,因为服务器可能只是阻止您)。
看看这个示例代码就明白我的意思了:
The only method I can think of is loading e.g. an image file from the external server. When that load fails, you "know" the server isn't responding (you actually don't know, because the server could just be blocking you).
Take a look at this example code to see what I mean:
我受到最新评论的启发,所以我写了这段快速代码。
这是一种“HTTP ping”,我认为与 XMLHttpRequest 调用()一起使用非常有用,例如找出在某些情况下使用最快的服务器或从用户的互联网收集一些粗略的统计数据连接速度。
这个小函数只是连接到一个不存在的 URL 上的 HTTP 服务器(预计返回 404),然后测量服务器响应 HTTP 请求的时间,并对累积时间求平均值和迭代次数。
所请求的 URL 在每次调用时都会随机修改,因为我注意到(可能)某些透明代理或缓存机制在某些情况下会伪造结果,从而给出额外的快速答案(实际上比 ICMP 更快,这有点奇怪)。
请注意使用适合真实 HTTP 服务器的 FQDN!
结果将显示到 id 为“result”的主体元素,例如:
功能代码:
例如,启动方式为:
请注意,我无法找到此脚本的结果数字与相应相同服务器上的 ICMP ping 之间的简单相关性,尽管 HTTP 响应时间似乎与 ICMP 响应时间大致成指数关系。这可以通过 HTTP 请求传输的数据量来解释,该数据量可能会根据 Web 服务器风格和配置、服务器本身的速度以及可能的其他原因而变化。
这不是很好的代码,但我认为它可以帮助并可能激励其他人。
I was inspired by the latest comment, so I wrote this quick piece of code.
This is a kind of "HTTP ping" which I think can be quite useful to use along with XMLHttpRequest calls(), for instance to figure out which is the fastest server to use in some case or to collect some rough statistics from the user's internet connexion speed.
This small function is just connecting to an HTTP server on an non-existing URL (that is expected to return a 404), then is measuring the time until the server is answering to the HTTP request, and is doing an average on the cumulated time and the number of iterations.
The requested URL is modified randomely at each call since I've noticed that (probably) some transparent proxies or caching mechanisms where faking results in some cases, giving extra fast answers (faster than ICMP actually which somewhat weird).
Beware to use FQDNs that fit a real HTTP server!
Results will display to a body element with id "result", for instance:
Function code:
For instance, launch with:
Note that I couldn't find a simple corelation between result figures from this script and the ICMP ping on the corresponding same servers, though HTTP response time seems to be roughly-exponential from ICMP response time. This may be explained by the amount of data that is transfered through the HTTP request which can vary depending on the web server flavour and configuration, obviously the speed of the server itself and probably other reasons.
This is not very good code but I thought it could help and possibly inspire others.
在 JS 中最接近 ping 的方法是使用 AJAX,并检索就绪状态、状态和标头。像这样:
当然,您也可以为不同的http状态添加条件,并通过描述等使输出显示您想要的,以使其看起来更好。更像是一个 http url 状态检查器而不是 ping,但实际上是相同的想法。你总是可以循环它几次,让它感觉更像你的 ping :)
The closest you're going to get to a ping in JS is using AJAX, and retrieving the readystates, status, and headers. Something like this:
Of course you can also put conditions in for different http statuses, and make the output display however you want with descriptions etc, to make it look nicer. More of an http url status checker than a ping, but same idea really. You can always loop it a few times to make it feel more like a ping for you too :)
我想出了一些东西,因为我厌倦了几个小时又一个小时地寻找每个人都说“不可能”的东西,我唯一发现的就是使用 jQuery。
我想出了一种新的简单方法,使用 Vanilla JS(除了基本 JavaScript)。
这是我的 JSFiddle: https://jsfiddle.net/TheNolle/5qjpmrxg/74/
基本上,我创建一个名为“start”的变量,我给它时间戳,然后我尝试将不可见图像的源设置到我的网站(这不是图像)[可以更改为任何网站],因为它不是图像创建一个错误,我用它来执行代码的第二部分,此时我创建一个名为“end”的新变量,我从这里给出时间戳(与“start”不同)。然后,我简单地做一个减法(我从“结束”中减去“开始”),这给了我 ping 这个网站所需的延迟。
选择后,您可以将其存储在一个值中,将其粘贴到网页上,粘贴到控制台等。
I've come up with something cause I was bored of searching hours after hours for something that everyone is saying "impossible", only thing I've found was using jQuery.
I've came up with a new simple way using Vanilla JS (nothing else than base JavaScript).
Here's my JSFiddle: https://jsfiddle.net/TheNolle/5qjpmrxg/74/
Basically, I create a variable called "start" which I give the timestamp, then I try to set an invisible image's source to my website (which isn't an image) [can be changed to any website], because it's not an image it creates an error, which I use to execute the second part of the code, at this time i create a new variable called "end" which i give the timestamp from here (which is different from "start"). Afterward, I simply make a substraction (i substract "start" from "end") which gives me the latency that it took to ping this website.
After you have the choice you can store that in a value, paste it on your webpage, paste it in the console, etc.
上面 ping 给定的 URL。
通常用于计数器/分析。
不会遇到客户端响应失败的情况(javascript)
Above pings the given Url.
Generally used for counters / analytics.
It won't encounter failed responses to client(javascript)
我建议使用“head”仅请求标头。
xhr.open('head', 'asstes/imgPlain/pixel.txt' + cacheBuster(), true);
并请求就绪状态 2 - HEADERS_RECEIVED send() 已被调用,并且标头和状态可用。
I suggest using "head" to request the header only.
xhr.open('head', 'asstes/imgPlain/pixel.txt' + cacheBuster(), true);
and than ask for readystate 2 - HEADERS_RECEIVED send() has been called, and headers and status are available.
您无法使用 JavaScript 进行 PING。我创建了 Java servlet,如果还活着,则返回 10x10 像素的绿色图像;如果死了,则返回红色图像。 https://github.com/pla1/Misc/blob/master/README.md
You can't PING with Javascript. I created Java servlet that returns a 10x10 pixel green image if alive and a red image if dead. https://github.com/pla1/Misc/blob/master/README.md