use*_*885 9 javascript latency ping
我想运行一个JavaScript代码来ping 4个不同的IP地址,然后检索这些ping请求的丢包和延迟,并在页面上显示它们.
我该怎么做呢?
Pis*_*3.0 19
你不能从JS做到这一点.你能做的是:
client --AJAX-- yourserver --ICMP ping-- targetservers
Run Code Online (Sandbox Code Playgroud)
向服务器发出AJAX请求,然后为您ping目标服务器,并在AJAX结果中返回结果.
可能的警告:
我能想到的唯一方法是从外部服务器加载例如图像文件。当加载失败时,您“知道”服务器没有响应(您实际上不知道,因为服务器可能只是阻止了您)。
看看这个示例代码,看看我的意思:
/*note that this is not an ICMP ping - but a simple HTTP request
giving you an idea what you could do . In this simple implementation it has flaws
as Piskvor correctly points out below */
function ping(extServer){
var ImageObject = new Image();
ImageObject.src = "http://"+extServer+"/URL/to-a-known-image.jpg"; //e.g. logo -- mind the caching, maybe use a dynamic querystring
if(ImageObject.height>0){
alert("Ping worked!");
} else {
alert("Ping failed :(");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62633 次 |
| 最近记录: |