如何知道网络是否已连接? [英] How to know if the network is (dis)connected?
问题描述
我如何知道,在Xul中,网络是否已连接?
- 更新
使用:
函数观察(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(观察,network:offline-status-changed,false);
和首选项:
pref(network.manage-offline-status,true);
它不起作用..有一个)
- 更新
但是,因为它不是一个可靠的解决方案(因为你无法依赖图像永远在网站上) ),最好的解决方案可能是开发一个新的XPCom组件。
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":
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.
这篇关于如何知道网络是否已连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!