2014-11-05 14:47:38 +00:00
|
|
|
# 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
|
2015-11-12 13:20:09 +00:00
|
|
|
const electron = require('electron');
|
|
|
|
const app = electron.app;
|
|
|
|
const BrowserWindow = electron.BrowserWindow;
|
2014-11-05 14:47:38 +00:00
|
|
|
|
2016-05-04 18:11:51 +00:00
|
|
|
let onlineStatusWindow;
|
|
|
|
|
|
|
|
app.on('ready', () => {
|
2014-11-05 14:47:38 +00:00
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false });
|
2016-05-04 18:11:51 +00:00
|
|
|
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`);
|
2014-11-05 14:47:38 +00:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
_online-status.html_
|
|
|
|
|
|
|
|
```html
|
2015-03-04 13:29:02 +00:00
|
|
|
<!DOCTYPE html>
|
2014-11-05 14:47:38 +00:00
|
|
|
<html>
|
2015-11-10 08:48:24 +00:00
|
|
|
<body>
|
|
|
|
<script>
|
2016-05-04 18:11:51 +00:00
|
|
|
const alertOnlineStatus = () => {
|
2015-11-10 08:48:24 +00:00
|
|
|
window.alert(navigator.onLine ? 'online' : 'offline');
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('online', alertOnlineStatus);
|
|
|
|
window.addEventListener('offline', alertOnlineStatus);
|
|
|
|
|
|
|
|
alertOnlineStatus();
|
|
|
|
</script>
|
|
|
|
</body>
|
2014-11-05 14:47:38 +00:00
|
|
|
</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
|
2015-03-26 15:20:31 +00:00
|
|
|
to the main process and handled as needed, as shown in the following example.
|
2014-11-05 14:47:38 +00:00
|
|
|
|
|
|
|
_main.js_
|
|
|
|
|
|
|
|
```javascript
|
2015-11-12 13:20:09 +00:00
|
|
|
const electron = require('electron');
|
|
|
|
const app = electron.app;
|
|
|
|
const ipcMain = electron.ipcMain;
|
|
|
|
const BrowserWindow = electron.BrowserWindow;
|
2014-11-05 14:47:38 +00:00
|
|
|
|
2016-05-04 18:11:51 +00:00
|
|
|
let onlineStatusWindow;
|
|
|
|
|
|
|
|
app.on('ready', () => {
|
2014-11-05 14:47:38 +00:00
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false });
|
2016-05-04 18:11:51 +00:00
|
|
|
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`);
|
2014-11-05 14:47:38 +00:00
|
|
|
});
|
|
|
|
|
2016-05-04 18:11:51 +00:00
|
|
|
ipcMain.on('online-status-changed', (event, status) => {
|
2014-11-05 14:47:38 +00:00
|
|
|
console.log(status);
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
_online-status.html_
|
|
|
|
|
|
|
|
```html
|
2015-03-04 13:29:02 +00:00
|
|
|
<!DOCTYPE html>
|
2014-11-05 14:47:38 +00:00
|
|
|
<html>
|
2015-11-10 08:48:24 +00:00
|
|
|
<body>
|
|
|
|
<script>
|
2016-05-04 18:11:51 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
|
|
|
const updateOnlineStatus = () => {
|
2015-11-10 08:48:24 +00:00
|
|
|
ipcRenderer.send('online-status-changed', navigator.onLine ? 'online' : 'offline');
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('online', updateOnlineStatus);
|
|
|
|
window.addEventListener('offline', updateOnlineStatus);
|
|
|
|
|
|
|
|
updateOnlineStatus();
|
|
|
|
</script>
|
|
|
|
</body>
|
2014-11-05 14:47:38 +00:00
|
|
|
</html>
|
|
|
|
```
|