在线/离线事件探测

概览

Online and offline event detection can be implemented in the Renderer process using the navigator.onLine attribute, part of standard HTML5 API.

The navigator.onLine attribute returns:

  • false if all network requests are guaranteed to fail (e.g. when disconnected from the network).
  • true in all other cases.

Since many cases return true, you should treat with care situations of getting false positives, as we cannot always assume that true value means that Electron can access the Internet. For example, in cases when the computer is running a virtualization software that has virtual Ethernet adapters in “always connected” state. Therefore, if you want to determine the Internet access status of Electron, you should develop additional means for this check.

示例

Event detection in the Renderer process

Starting with a working application from the Quick Start Guide, update the main.js file with the following lines:

  1. const { app, BrowserWindow } = require('electron')
  2. let onlineStatuswindow
  3. app.whenReady().then(() => }
  4. onlineStatuswindow = new BrowserWindow({ width: 0, height: 0, show: false })
  5. onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
  6. })

create the online-status.html file and add the following line before the closing </body> tag:

  1. <script src="renderer.js"></script>

并添加 渲染器.js 文件:

  1. const alertOnlineStatus = () => { window.alert(navigator.onLine ? 'online' : 'offline') }
  2. window.addEventListener('online', alertOnlineStatus)
  3. window.addEventListener('offline', alertOnlineStatus)
  4. alertOnlineStatus()

启动 Electron 应用程序后,您应该看到通知:

Online-offline-event detection

Event detection in the Main process

There may be situations when you want to respond to online/offline events in the Main process as well. The Main process, however, does not have a navigator object and cannot detect these events directly. In this case, you need to forward the events to the Main process using Electron’s inter-process communication (IPC) utilities.

Starting with a working application from the Quick Start Guide, update the main.js file with the following lines:

  1. const { app, BrowserWindow, ipcMain } = require('electron')
  2. let onlineStatuswindow
  3. app.whenReady(). hen(() =>
  4. OnlineStatuswindow = 新的 BrowserWindow(。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。) 显示: false, web首选项: { nodeIntegration: true } })
  5. onlineStatusWindow. oadURL(`file://${__dirname}/online-status.html`)
  6. })
  7. ipcMain.on('online-status-changed', (events, status) => *
  8. console.log(status)
  9. })

create the online-status.html file and add the following line before the closing </body> tag:

  1. <script src="renderer.js"></script>

并添加 渲染器.js 文件:

  1. const { ipcRenderer } = require('electron')
  2. const updateOnlineStatus = () => { ipcRenderer.send('online-status-changed', navigator.onLine ? 'online' : 'offline') }
  3. window.addEventListener('online', updateOnlineStatus)
  4. window.addEventListener('offline', updateOnlineStatus)
  5. updateOnlineStatus()

After launching the Electron application, you should see the notification in the Console:

  1. npm start
  2. > electron@1.0.0 start /electron
  3. > electron .
  4. online