简体   繁体   English

在 Electron 中检测互联网连接

[英]Detect internet connection in Electron

Our Electron app needs some configuration files and usually downloads them from our server.我们的 Electron 应用程序需要一些配置文件,通常从我们的服务器下载它们。 In case the client is offline, we don't want him to see any error messages.如果客户端离线,我们不希望他看到任何错误消息。 We are using Electron Download Manager to get the files.我们正在使用电子下载管理器来获取文件。

This is how we request the files这就是我们请求文件的方式

try{
    DownloadManager.bulkDownload({
        urls: links
    }, function (error, finished, errors) {
        if (error) {
            console.log("ERROR: Encountered error during config file download!");
            console.log("Finished Files: " + finished);
            console.log("Errors: " + errors);
            return;
        }
        console.log("Finished loading all configuration files.");
    });
} catch (err){
    online_status = false;
    console.log("Couldn't get online configuration. Starting app as offline.");
}

With this code the error Uncaught Exception: Error: net::ERR_ADDRESS_UNREACHABLE is thrown, when not connected to the internet.使用此代码,当未连接到互联网时,会抛出错误Uncaught Exception: Error: net::ERR_ADDRESS_UNREACHABLE

I tried implementing the official event detection from Electron but as they state:我尝试从 Electron实现官方事件检测,但正如他们所说:

Such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always “connected.”例如在计算机运行虚拟化软件的情况下,该软件具有始终“连接”的虚拟以太网适配器。 Therefore, if you really want to determine the internet access status of Electron, you should develop additional means for checking.因此,如果你真的想确定 Electron 的互联网访问状态,你应该开发额外的检查手段。

Then I went on trying just to ping the server like in this thread but this doesn't work at all for me, it's possible that it doesn't work anymore.然后我继续尝试像在这个线程中一样 ping 服务器,但这对我来说根本不起作用,它可能不再起作用了。

This is a bug in Electron Download Manager, documented in an GitHub Issue like @JeffRSon stated.这是 Electron 下载管理器中的一个错误,记录在一个GitHub 问题中,如@JeffRSon 所述。 A pull request is ready for a merge but not merged yet (posted 06.06.2019). 拉取请求已准备好进行合并但尚未合并(2019 年 6 月 6 日发布)。

I hope this helps you.我希望这可以帮助你。 I will update this question if it get's merged and works.如果它被合并并有效,我会更新这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM