electron/docs/tutorial/online-offline-events.md

90 lines
2.5 KiB
Markdown
Raw Normal View History

# Online/Offline Event Detection
Online and offline event detection can be implemented in the renderer process
using standard HTML5 APIs, as shown in the following example.
_main.js_
```javascript
const {app, BrowserWindow} = require('electron')
let onlineStatusWindow
app.on('ready', () => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
})
```
_online-status.html_
```html
<!DOCTYPE html>
<html>
2015-11-10 08:48:24 +00:00
<body>
<script>
const alertOnlineStatus = () => {
window.alert(navigator.onLine ? 'online' : 'offline')
}
2015-11-10 08:48:24 +00:00
window.addEventListener('online', alertOnlineStatus)
window.addEventListener('offline', alertOnlineStatus)
2015-11-10 08:48:24 +00:00
alertOnlineStatus()
2015-11-10 08:48:24 +00:00
</script>
</body>
</html>
```
2015-09-01 02:17:59 +00:00
There may be instances where you want to respond to these events in the
main process as well. The main process however does not have a
`navigator` object and thus cannot detect these events directly. Using
2015-04-16 03:31:12 +00:00
Electron's inter-process communication utilities, the events can be forwarded
to the main process and handled as needed, as shown in the following example.
_main.js_
```javascript
const {app, BrowserWindow, ipcMain} = require('electron')
let onlineStatusWindow
app.on('ready', () => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
})
ipcMain.on('online-status-changed', (event, status) => {
console.log(status)
})
```
_online-status.html_
```html
<!DOCTYPE html>
<html>
2015-11-10 08:48:24 +00:00
<body>
<script>
const {ipcRenderer} = require('electron')
const updateOnlineStatus = () => {
ipcRenderer.send('online-status-changed', navigator.onLine ? 'online' : 'offline')
}
2015-11-10 08:48:24 +00:00
window.addEventListener('online', updateOnlineStatus)
window.addEventListener('offline', updateOnlineStatus)
2015-11-10 08:48:24 +00:00
updateOnlineStatus()
2015-11-10 08:48:24 +00:00
</script>
</body>
</html>
```
2016-10-13 14:42:44 +00:00
**NOTE:** If Electron is not able to connect to a local area network (LAN) or
a router, it is considered offline; all other conditions return true.
So while you can assume that the browser is offline when `navigator.onLine`
returns a false value, you cannot assume that a true value necessarily
means that the browser can access the internet. You could be getting false
positives, 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 online status of the browser,
you should develop additional means for checking.