如标题,实时监听网络情况,如果没有网络情况就会显示脱机,请问node或者electron是否可以做到?求教
如标题,实时监听网络情况,如果没有网络情况就会显示脱机,请问node或者electron是否可以做到?求教
监听navigator.onLine可以实现,感谢两位的帮助
window.addEventListener("offline", function(e) {
alert("offline")
})
window.addEventListener("online", function(e) {
alert("online")
})
function isOnline(user_callback){
/**
* Show a warning to the user.
* You can retry in the dialog until a internet connection
* is active.
*/
var message = function(){
const {dialog} = require('electron').remote;
return dialog.showMessageBox({
title:"There's no internet",
message:"No internet available, do you want to try again?",
type:'warning',
buttons:["Try again please","I don't want to work anyway"],
defaultId: 0
},function(index){
// if clicked "Try again please"
if(index == 0){
execute();
}
})
};
var execute = function(){
if(navigator.onLine){
// Execute action if internet available.
user_callback();
}else{
// Show warning to user
// And "retry" to connect
message();
}
};
// Verify for first time
execute();
}
// Use it, the alert("Hello world"); will be executed only if there's an active internet connection.
isOnline(function(){
alert("Hello world !");
});
you can check out this blog for details.
他山跨平台混合应用开发框架(OHUI),是使用Gecko(v1.9~v52)嵌入,实现跨平台混合应用开发的框架。支持xp/2003/win7 x32,x64/win8+...,Linux,Android系统,支持xul, html(5), css(3), js,c/c++/java混合编程开发界面。OHUI v22.0发行包大小13MB(Linux下21MB).
5 回答4.8k 阅读✓ 已解决
4 回答2.5k 阅读✓ 已解决
2 回答2.5k 阅读✓ 已解决
1 回答2.2k 阅读✓ 已解决
2 回答1.7k 阅读✓ 已解决
5 回答1.9k 阅读
2 回答1.3k 阅读✓ 已解决
试试navigator.onLine,不需要node.js不需要electron,普通网页都能判断是否有网络