阅读背景:

如果可以访问互联网(谷歌,aws等),最低级的测试方式

来源:互联网 

I'm looking for a URL I can use to test if the internet is up.

我正在寻找一个可以用来测试互联网是否正常运行的网址。

I can test my server with a 1x1 pixel (recommended below), but how to I test if the internet is up, but my server is down.

我可以使用1x1像素(下面推荐)测试我的服务器,但如何测试互联网是否已启动,但我的服务器已关闭。

In my app, I can use these two events about the local network interface (wifi AP), but not the internet (gateway, dns, upstream network):

在我的应用程序中,我可以使用这两个关于本地网络接口(wifi AP)的事件,但不能使用互联网(网关,DNS,上游网络):

window.addEventListener('online', onNetworkUp, false);
window.addEventListener('offline', onNetworkDown, false);

So, I can be "online", but not connected to the internet.

所以,我可以“在线”,但没有连接到互联网。

To test "internet", I can ping my API server, but also want to check somewhere else who may have a better SLA. E.g. google, amazon, etc.

为了测试“互联网”,我可以ping我的API服务器,但也想检查可能有更好的SLA的其他地方。例如。谷歌,亚马逊等

What's the least rude way to do this? Is there a URL that expects to be used this way?

这样做最不礼貌的方法是什么?是否有一个希望以这种方式使用的URL?

Mike

麦克风

2 个解决方案

#1


1  

The easiest way to check if you are connected to the internet is using navigator.onLine:

检查您是否连接到互联网的最简单方法是使用navigator.onLine:

if (navigator.onLine) {
    // you are online... do something
} else {
    // you're not online
}

However this will not work for your local server, so my second favourite way (since that there are many ways of doing this) is to use any web request, (e.g. an image) adding an onerror event:

但是这对你的本地服务器不起作用,所以我的第二个喜欢的方式(因为有很多方法)是使用添加onerror事件的任何web请求(例如图像):

<img src='https://www.example.com/singlepixel.gif' onerror='offline();' />
<script>
    function offline() {
        alert('Internet offline!');
    }
</script>

Obviously you should make a request to a little image, 1x1px for example, so that when you're connected to the internet this doesn't take too long to load.

显然你应该向一个小图像请求,例如1x1px,这样当你连接到互联网时,加载时间不会太长。

Or if you want to use pure Javascript:

或者如果你想使用纯Javascript:

function internetON() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'https://www.example.com/singlepixel.gif', false);
    xhr.send();
    return xhr.status === 200;
}

if (internetON()) {
    // you are online... do something
} else {
    // you're not online
}

#2


1  

Here's what I found. There isn't a good way to get this info from the browser. Browsers overload "online" to make the implementations meaningless.

这是我发现的。没有一种从浏览器获取此信息的好方法。浏览器超载“在线”以使实现无意义。

My use case is this: I want to know if the app can reach my server, and offer different behavior if it can't.

我的用例是:我想知道应用程序是否可以访问我的服务器,如果不能,则提供不同的行为。

I had two ways to do this.

我有两种方法可以做到这一点。

  • For PouchDB replication, look for failures, and assume offline it's failing - restart as necessary. I imagine socket.io has a similar feature.

    对于PouchDB复制,查找失败,并假设脱机失败 - 根据需要重新启动。我想socket.io有一个类似的功能。

  • When I'm not PouchDB replicating, but wanting to demonstrate the offline-ness of the app, I added a route to my Express server /r_u_up and polled with this library offline.js

    当我不是PouchDB复制,但想要演示应用程序的离线时,我添加了一条到我的Express服务器/ r_u_up的路由,并使用此库离线轮询.js

A last note is /r_u_up could be implemented server-side in Nginx so it doesn't even need Node.js. I haven't implemented this yet, but the approach would be similar to enabling the pre-flight OPTIONS requests. It should also work with other web servers.

最后一点是/ r_u_up可以在Nginx的服务器端实现,因此它甚至不需要Node.js.我还没有实现这个,但这种方法类似于启用飞行前OPTIONS请求。它也应该与其他Web服务器一起使用。

That's what I learn-inated!

这就是我学到的东西!


分享到: