如何知道网络是否连接(断开)?

发布于 2024-10-19 19:29:02 字数 959 浏览 4 评论 0原文

在 Xul 中,我如何知道网络是否已连接(断开)?

--update

使用:

    function observe(aSubject, aTopic, aState) {
        if (aTopic == "network:offline-status-changed") {
            write("STATUS CHANGED!");
        }
    }
    var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
    os.addObserver(observe, "network:offline-status-changed", false);

和首选项:

pref("network.manage-offline-status", true);

它不起作用..有一个 错误报告在这里,但我认为这与此无关。

--

实际上我认为不可能收到通知,因为即使在 Firefox 中我们也不会收到通知,并且如果用户希望浏览器知道其处于离线状态,则需要手动标记“离线工作” ..

--

Firefox“about:config”过滤“offline”字符串的屏幕截图,不幸的是,没有“network.manage-offline-status”:

在此处输入图像描述

How can I know, in Xul, if the network is (dis)connected?

--update

Using:

    function observe(aSubject, aTopic, aState) {
        if (aTopic == "network:offline-status-changed") {
            write("STATUS CHANGED!");
        }
    }
    var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
    os.addObserver(observe, "network:offline-status-changed", false);

and the preference:

pref("network.manage-offline-status", true);

it's not working.. There's a bug report here, but I don't think it has something to do with it.

--

Actually I think it's not possible to be notified, as even in Firefox we're never notified, and the user need to manually mark "work offline" if he wants the browser to know that it's offline..

--

Screenshot my of Firefox "about:config" filtering for "offline" string, unfortunately, there no "network.manage-offline-status":

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无力看清 2024-10-26 19:29:02

您应该能够使用 navigator.onLine。这是帮助页面

https://developer.mozilla.org/en/Online_and_offline_events

navigator.onLine 是一个属性
保持真/假值(真为
在线,离线则为 false)。这
每当用户更新属性
切换到“离线模式”
选择相应的菜单项
(文件 -> 在 Firefox 中离线工作)。

另一个解决方案(如@Neil 评论):

Components.classes["@mozilla.org/observer-service;1"]
    .getService(Components.interfaces.nsIObserverService)
    .addObserver(myF­unction, "network:offline-status-changed", false);

You should be able to use navigator.onLine. Here is the help page

https://developer.mozilla.org/en/Online_and_offline_events

navigator.onLine is a property that
maintains a true/false value (true for
online, false for offline). This
property is updated whenever the user
switches into "Offline Mode" by
selecting the corresponding menu item
(File -> Work Offline in Firefox).

Another solution (as commented by @Neil):

Components.classes["@mozilla.org/observer-service;1"]
    .getService(Components.interfaces.nsIObserverService)
    .addObserver(myF­unction, "network:offline-status-changed", false);
完美的未来在梦里 2024-10-26 19:29:02

我发现最好的方法是使用下面的 javascript 代码,其行为类似于 ping,并在一些大网站上进行测试,并假设如果它们都没有响应,那么网络必须断开。

var ping = {};
ping = {
    img:null,
    imgPreload:null,
    timer:null,
    init:function() {
        var sess = new Date();
        var nocache = sess.getTime();
        var imguri = ping.img+"?time="+nocache;
        var ping.imgPreload = new Image();
        ping.imgPreload.onload = function() {
            clearTimeout(ping.timer);
            ping.timer = null;
            alert("Domain is available");
        };
        ping.imgPreload.src = imguri;
        ping.timer = setTimeout("ping.fail_to_ping()",60000);
    },
    fail_to_ping:function() {
        clearTimeout(ping.timer);
        ping.timer = null;
        ping.imgPreload = null;
        alert("Ping to domain failed!");
    }
};

(来自 http://crynobone.com/ci/index.php/archive/view /852)

--update

但是,由于这不是一个可靠的解决方案(因为您不能相信该图像将永远存在于网站中),最好的解决方案可能是开发一个新的 XPCom 组件。

The best way I found is to use the following javascript code, that behaves like a ping, and make the test with some big websites, and assume that if none of them answers, so the network must be disconnected.

var ping = {};
ping = {
    img:null,
    imgPreload:null,
    timer:null,
    init:function() {
        var sess = new Date();
        var nocache = sess.getTime();
        var imguri = ping.img+"?time="+nocache;
        var ping.imgPreload = new Image();
        ping.imgPreload.onload = function() {
            clearTimeout(ping.timer);
            ping.timer = null;
            alert("Domain is available");
        };
        ping.imgPreload.src = imguri;
        ping.timer = setTimeout("ping.fail_to_ping()",60000);
    },
    fail_to_ping:function() {
        clearTimeout(ping.timer);
        ping.timer = null;
        ping.imgPreload = null;
        alert("Ping to domain failed!");
    }
};

(from http://crynobone.com/ci/index.php/archive/view/852)

--update

But, as it's not a reliable solution (as you can't rely that the image will be in the website forever), the best solution might be to develop a new XPCom component.

呃...根据 HTML5(请阅读 echmascript 5),on-/offline 事件可用。

请参阅 Mozilla Hacks

编辑 20/4/2011:< br>
当我观看 MS MIX11 的播客时,我刚刚遇到了这个答案的更新:
http://channel9.msdn.com/Events/MIX/MIX11/HTM14 43:36 左右,讲师实际上正在谈论 window.navigator.onLine 属性,他使用它来检测浏览器(和计算机)是否在线。然后,当他再次上线时,他使用 online 事件执行某些操作。

但是,此方法仅在现代浏览器中可用。因此 IE 8 及以下版本必须轮询连接。

Eh... as per HTML5 (read echmascript 5), the on-/offline events are available.

See it here at Mozilla Hacks

Edit 20/4/2011:
I just encountered an update for this answer, when i was watching a podcast from MS MIX11:
http://channel9.msdn.com/Events/MIX/MIX11/HTM14 around time 43:36, the lecturer is actually talking about the window.navigator.onLine property, where he uses it for detecting if the browser (and the computer) is online. Then he uses the online event to do something when he gets online again.

This method is only available in modern browsers, however. So IE 8 and below have to poll for the connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文