2014-11-05 14:47:38 +00:00
|
|
|
# Online/Offline Event Detection
|
|
|
|
|
2020-10-21 06:46:56 +00:00
|
|
|
## Overview
|
2017-10-18 10:58:18 +00:00
|
|
|
|
2020-10-21 06:46:56 +00:00
|
|
|
[Online and offline event](https://developer.mozilla.org/en-US/docs/Online_and_offline_events)
|
|
|
|
detection can be implemented in the Renderer process using the
|
|
|
|
[`navigator.onLine`](http://html5index.org/Offline%20-%20NavigatorOnLine.html)
|
|
|
|
attribute, part of standard HTML5 API.
|
2014-11-05 14:47:38 +00:00
|
|
|
|
2020-10-21 06:46:56 +00:00
|
|
|
The `navigator.onLine` attribute returns:
|
2020-11-05 22:12:43 +00:00
|
|
|
|
2020-10-21 06:46:56 +00:00
|
|
|
* `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.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
### Event detection in the Renderer process
|
|
|
|
|
|
|
|
Starting with a working application from the
|
|
|
|
[Quick Start Guide](quick-start.md), update the `main.js` file
|
|
|
|
with the following lines:
|
2014-11-05 14:47:38 +00:00
|
|
|
|
|
|
|
```javascript
|
2018-09-13 16:10:51 +00:00
|
|
|
const { app, BrowserWindow } = require('electron')
|
2014-11-05 14:47:38 +00:00
|
|
|
|
2016-07-26 01:39:25 +00:00
|
|
|
let onlineStatusWindow
|
2016-05-04 18:11:51 +00:00
|
|
|
|
2020-02-03 22:43:22 +00:00
|
|
|
app.whenReady().then(() => {
|
2016-07-26 01:39:25 +00:00
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
|
2020-11-30 07:48:39 +00:00
|
|
|
onlineStatusWindow.loadURL(`file://${__dirname}/index.html`)
|
2016-07-26 01:39:25 +00:00
|
|
|
})
|
2014-11-05 14:47:38 +00:00
|
|
|
```
|
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
in the `index.html` file, add the following line before the
|
2020-10-21 06:46:56 +00:00
|
|
|
closing `</body>` tag:
|
2014-11-05 14:47:38 +00:00
|
|
|
|
|
|
|
```html
|
2020-10-21 06:46:56 +00:00
|
|
|
<script src="renderer.js"></script>
|
|
|
|
```
|
|
|
|
|
|
|
|
and add the `renderer.js` file:
|
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
```javascript fiddle='docs/fiddles/features/online-detection/renderer'
|
2020-10-21 06:46:56 +00:00
|
|
|
const alertOnlineStatus = () => { window.alert(navigator.onLine ? 'online' : 'offline') }
|
|
|
|
|
|
|
|
window.addEventListener('online', alertOnlineStatus)
|
|
|
|
window.addEventListener('offline', alertOnlineStatus)
|
|
|
|
|
|
|
|
alertOnlineStatus()
|
2014-11-05 14:47:38 +00:00
|
|
|
```
|
|
|
|
|
2020-10-21 06:46:56 +00:00
|
|
|
After launching the Electron application, you should see the notification:
|
|
|
|
|
|
|
|
![Online-offline-event detection](../images/online-event-detection.png)
|
|
|
|
|
|
|
|
### Event detection in the Main process
|
2014-11-05 14:47:38 +00:00
|
|
|
|
2020-10-21 06:46:56 +00:00
|
|
|
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](quick-start.md), update the `main.js` file
|
|
|
|
with the following lines:
|
2014-11-05 14:47:38 +00:00
|
|
|
|
|
|
|
```javascript
|
2018-09-13 16:10:51 +00:00
|
|
|
const { app, BrowserWindow, ipcMain } = require('electron')
|
2016-07-26 01:39:25 +00:00
|
|
|
let onlineStatusWindow
|
2016-05-04 18:11:51 +00:00
|
|
|
|
2020-02-03 22:43:22 +00:00
|
|
|
app.whenReady().then(() => {
|
2020-01-13 05:56:39 +00:00
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false, webPreferences: { nodeIntegration: true } })
|
2020-11-30 07:48:39 +00:00
|
|
|
onlineStatusWindow.loadURL(`file://${__dirname}/index.html`)
|
2016-07-26 01:39:25 +00:00
|
|
|
})
|
2014-11-05 14:47:38 +00:00
|
|
|
|
2016-05-04 18:11:51 +00:00
|
|
|
ipcMain.on('online-status-changed', (event, status) => {
|
2016-07-26 01:39:25 +00:00
|
|
|
console.log(status)
|
|
|
|
})
|
2014-11-05 14:47:38 +00:00
|
|
|
```
|
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
in the `index.html` file, add the following line before the
|
2020-10-21 06:46:56 +00:00
|
|
|
closing `</body>` tag:
|
2014-11-05 14:47:38 +00:00
|
|
|
|
|
|
|
```html
|
2020-10-21 06:46:56 +00:00
|
|
|
<script src="renderer.js"></script>
|
|
|
|
```
|
|
|
|
|
|
|
|
and add the `renderer.js` file:
|
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
```javascript fiddle='docs/fiddles/features/online-detection/main'
|
2020-10-21 06:46:56 +00:00
|
|
|
const { ipcRenderer } = require('electron')
|
|
|
|
const updateOnlineStatus = () => { ipcRenderer.send('online-status-changed', navigator.onLine ? 'online' : 'offline') }
|
|
|
|
|
|
|
|
window.addEventListener('online', updateOnlineStatus)
|
|
|
|
window.addEventListener('offline', updateOnlineStatus)
|
|
|
|
|
|
|
|
updateOnlineStatus()
|
|
|
|
```
|
|
|
|
|
|
|
|
After launching the Electron application, you should see the notification in the
|
|
|
|
Console:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
npm start
|
|
|
|
|
|
|
|
> electron@1.0.0 start /electron
|
|
|
|
> electron .
|
|
|
|
|
|
|
|
online
|
2014-11-05 14:47:38 +00:00
|
|
|
```
|