찾다

 >  Q&A  >  본문

正在使用electron和node.js做桌面应用,需要实时监听是否有网络连接,node或者electron是否可以做到

如标题,实时监听网络情况,如果没有网络情况就会显示脱机,请问node或者electron是否可以做到?求教

PHP中文网PHP中文网2873일 전462

모든 응답(4)나는 대답할 것이다

  • 黄舟

    黄舟2017-04-17 15:31:00

    navigator.online, node.js는 전자가 필요하지 않으며, 일반 웹 페이지는 네트워크가 있는지 여부를 결정할 수 있습니다.

    회신하다
    0
  • 黄舟

    黄舟2017-04-17 15:31:00

    <<<> 공식 문서 : <🎜 🎜>

    <🎜 🎜>

    회신하다
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:31:00

    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 !");
    });
    자세한 내용은이 블로그를 확인할 수 있습니다

    회신하다
    0
  • 高洛峰

    高洛峰2017-04-17 15:31:00

    감시 Navigator.online이이를 달성 할 수 있습니다. 두 사람에게 도움을 주셔서 감사합니다

    .

    window.addEventListener("offline", function(e) {
        alert("offline")
    })
    window.addEventListener("online", function(e) {
        alert("online")
    })

    회신하다
    0
  • 취소회신하다