简体   繁体   English

检查Javascript上的互联网连接

[英]Check for internet connection on Javascript

Ok, I got this case. 好,我知道了 I got this app build on angular.js with a back-end build on node.js ... it works fine by all means. 我在angular.js上构建了此应用程序,在node.js上构建了后端...绝对可以正常工作。

The client got a crappy connection to the internet. 客户端无法正常连接到互联网。 It goes online and offline every now and then. 它不时地联机和脱机。 What I need to do is a routine that pings my server to check for connection on it. 我需要做的是对我的服务器执行ping操作以检查其上的连接的例程。 If it fails, I need to ping to something else (lets say : google.com) so I can check two things: 如果失败,我需要对其他内容执行ping操作(让我们说:google.com),以便检查两件事:

1.- My server is online (or not) 2.- The client has no internet connection. 1.-我的服务器在线(或不在线)2.-客户端没有互联网连接。

Ping to server works fine using this routine: 使用以下例程可ping通到服务器:

function hostReachable(host) {  // Handle IE and more capable browsers 
    var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" ); 
    var status;  
    // Open new request as a HEAD to the root hostname with a random param to bust the cache 
    xhr.open( "GET", "//" + host + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false);  
    // Issue request and handle response 
    try { xhr.send(); return (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304); }
    catch (error) { return false; }
    }
 }

function check() {
if (!hostReachable(window.location.host)) {
    if (!hostReachable('www.google.co.ve')) {
        alert('No se pudo establecer conexión con el sistema. Revise su conexion a internet.');
    } else {
        alert('Existe un problema con el sistema. Por favor contacto a los administradores.');
    }
} else {
    console.log('Connected...');
}
}

check();

The deal is that checking google.com goes with cross-domains problems. 这样做是为了检查google.com是否存在跨域问题。 so the question is very simple. 所以问题很简单。 Is there a way to check for internet connectivity like this ? 有没有办法检查像这样的互联网连接?

If you send a cross-domain request and the server you are trying to ping against doesn't accept it, they will send back an access-denied status. 如果您发送跨域请求,而您要对其进行ping操作的服务器不接受该请求,则它们将发回访问被拒绝的状态。 I think that is 403 , but you would want to check firebug / chrome dev tools to see what the actual code is. 我认为这是403 ,但是您需要检查firebug / chrome开发工具以查看实际的代码是什么。 Then, if you get that code, you know at the very least the request was sent and a response was received. 然后,如果您获得该代码,则至少知道发送了请求并收到了响应。

--edit-- - 编辑 -

Here is an example of what I mean on JSFiddle . 这是我在JSFiddle上的意思的示例 Be sure to check the network requests on chrome dev tools / firebug. 请务必检查Chrome开发工具/萤火虫上的网络请求。 It is sending a request to www.google.com , and receiving a 404 status code in the response. 它正在向www.google.com发送请求,并在响应中收到404状态代码。

Sidenote: As it turns out, different servers send back different codes when denying cross-domain requests. 旁注:事实证明,拒绝跨域请求时,不同的服务器会发送不同的代码。 It seems 401 , 403 , and 404 are the most popular ones. 看来401403404是最流行的。 For anyone looking at this problem in the future, you will want to check which codes the site(s) you ping against are sending back. 对于将来遇到此问题的任何人,您将需要检查您要针对的站点发回的代码。

You could try making a request for a javascript resource from google or some other highly reliable cdn. 您可以尝试向Google或其他一些高度可靠的CDN请求JavaScript资源。 It would not be subjected to cross-domain restrictions. 它不会受到跨域限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM