Chu*_*ebs 150 javascript
我正在制作一个Web应用程序,要求我检查远程服务器是否在线.当我从命令行运行它时,我的页面加载速度达到了整整60秒(对于8个条目,它会随着更多线性线性扩展).
我决定在用户端进行ping操作.这样,我可以加载页面,让他们在浏览我的内容时等待"服务器在线"数据.
如果有人对上述问题有答案,或者他们知道解决方案以保持我的页面加载速度快,我肯定会感激.
Chu*_*ebs 129
我发现有人通过非常巧妙地使用本机Image对象来实现这一点.
从他们的来源来看,这是主要功能(它依赖于源的其他部分,但你明白了).
function Pinger_ping(ip, callback) {
if(!this.inUse) {
this.inUse = true;
this.callback = callback
this.ip = ip;
var _that = this;
this.img = new Image();
this.img.onload = function() {_that.good();};
this.img.onerror = function() {_that.good();};
this.start = new Date().getTime();
this.img.src = "http://" + ip;
this.timer = setTimeout(function() { _that.bad();}, 1500);
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于我测试过的所有类型的服务器(Web服务器,ftp服务器和游戏服务器).它也适用于端口.如果有人遇到失败的用例,请在评论中发帖,我会更新我的答案.
更新:上一个链接已被删除.如果有人发现或实施上述内容,请发表评论,我会将其添加到答案中.
更新2:@trante足以提供一个jsFiddle.
http://jsfiddle.net/GSSCD/203/
更新3:@Jonathon创建了一个带有实现的GitHub仓库.
https://github.com/jdfreder/pingjs
更新4:看起来这个实现不再可靠.人们还报告Chrome不再支持所有内容,从而引发net::ERR_NAME_NOT_RESOLVED错误.如果有人可以验证替代解决方案,我会将其作为接受的答案.
小智 20
Ping是ICMP,但如果远程服务器上有任何打开的TCP端口,则可以像这样实现:
function ping(host, port, pong) {
var started = new Date().getTime();
var http = new XMLHttpRequest();
http.open("GET", "http://" + host + ":" + port, /*async*/true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
var ended = new Date().getTime();
var milliseconds = ended - started;
if (pong != null) {
pong(milliseconds);
}
}
};
try {
http.send(null);
} catch(exception) {
// this is expected
}
}
Run Code Online (Sandbox Code Playgroud)
jer*_*jer 18
你可以试试这个:
在有或没有任何内容的服务器上放置ping.html,在javascript上执行如下操作:
<script>
function ping(){
$.ajax({
url: 'ping.html',
success: function(result){
alert('reply');
},
error: function(result){
alert('timeout/error');
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
Eug*_*ene 13
你不能在javascript中直接"ping".可能还有其他一些方法:
这里有很多疯狂的答案,尤其是关于 CORS 的答案 -
您可以执行 http HEAD 请求(类似于 GET 但没有有效负载)。请参阅https://ochronus.com/http-head-request-good-uses/
它不需要预检检查,混乱是由于旧版本的规范造成的,请参阅 为什么跨域 HEAD 请求需要预检检查?
所以你可以使用上面的答案,它使用 jQuery 库(没有说)但是
type: 'HEAD'
Run Code Online (Sandbox Code Playgroud)
--->
<script>
function ping(){
$.ajax({
url: 'ping.html',
type: 'HEAD',
success: function(result){
alert('reply');
},
error: function(result){
alert('timeout/error');
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
当然,你也可以使用 vanilla js 或 dojo 或其他......
小智 6
为了快速保持您的请求,请缓存ping的服务器端结果,并每隔几分钟更新一次ping文件或数据库(或者您希望它是否准确).您可以使用cron运行带有8个ping的shell命令并将输出写入文件,Web服务器将此文件包含在您的视图中.
使用websocket解决方案......
function ping(ip, isUp, isDown) {
var ws = new WebSocket("ws://" + ip);
ws.onerror = function(e){
isUp();
ws = null;
};
setTimeout(function() {
if(ws != null) {
ws.close();
ws = null;
isDown();
}
},2000);
}
Run Code Online (Sandbox Code Playgroud)
标准 ping 的问题在于它们是 ICMP,出于安全和流量原因,很多地方都不允许通过。这或许可以解释失败的原因。
1.9 之前的 Ruby 有一个基于 TCP 的ping.rb,它将与 Ruby 1.9+ 一起运行。您所要做的就是将其从 1.8.7 安装复制到其他地方。我刚刚通过 ping 我的家庭路由器确认它可以运行。
如果您要查看的是服务器是否“存在”,则可以使用以下命令:
function isValidURL(url) {
var encodedURL = encodeURIComponent(url);
var isValid = false;
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + encodedURL + "%22&format=json",
type: "get",
async: false,
dataType: "json",
success: function(data) {
isValid = data.query.results != null;
},
error: function(){
isValid = false;
}
});
return isValid;
}
Run Code Online (Sandbox Code Playgroud)
这将返回true / false指示服务器是否存在。
如果您需要响应时间,则可以进行一些修改:
function ping(url) {
var encodedURL = encodeURIComponent(url);
var startDate = new Date();
var endDate = null;
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + encodedURL + "%22&format=json",
type: "get",
async: false,
dataType: "json",
success: function(data) {
if (data.query.results != null) {
endDate = new Date();
} else {
endDate = null;
}
},
error: function(){
endDate = null;
}
});
if (endDate == null) {
throw "Not responsive...";
}
return endDate.getTime() - startDate.getTime();
}
Run Code Online (Sandbox Code Playgroud)
用法很简单:
var isValid = isValidURL("http://example.com");
alert(isValid ? "Valid URL!!!" : "Damn...");
Run Code Online (Sandbox Code Playgroud)
要么:
var responseInMillis = ping("example.com");
alert(responseInMillis);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
225477 次 |
| 最近记录: |