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
|
|
|
|
var app = require('app');
|
|
|
|
var BrowserWindow = require('browser-window');
|
|
|
|
var onlineStatusWindow;
|
|
|
|
|
|
|
|
app.on('ready', function() {
|
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false });
|
2014-11-05 18:21:18 +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>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
var alertOnlineStatus = function() {
|
|
|
|
window.alert(navigator.onLine ? 'online' : 'offline');
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('online', alertOnlineStatus);
|
|
|
|
window.addEventListener('offline', alertOnlineStatus);
|
|
|
|
|
|
|
|
alertOnlineStatus();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
```
|
|
|
|
|
|
|
|
There may be instances where one wants to respond to these events in the
|
2015-03-26 15:20:31 +00:00
|
|
|
main process as well. The main process however does not have a
|
2014-11-05 18:21:18 +00:00
|
|
|
`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
|
|
|
|
var app = require('app');
|
|
|
|
var ipc = require('ipc');
|
|
|
|
var BrowserWindow = require('browser-window');
|
|
|
|
var onlineStatusWindow;
|
|
|
|
|
|
|
|
app.on('ready', function() {
|
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false });
|
2014-11-05 18:21:18 +00:00
|
|
|
onlineStatusWindow.loadUrl('file://' + __dirname + '/online-status.html');
|
2014-11-05 14:47:38 +00:00
|
|
|
});
|
|
|
|
|
2014-11-05 18:21:18 +00:00
|
|
|
ipc.on('online-status-changed', function(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>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
var ipc = require('ipc');
|
|
|
|
var updateOnlineStatus = function() {
|
2014-11-05 18:21:18 +00:00
|
|
|
ipc.send('online-status-changed', navigator.onLine ? 'online' : 'offline');
|
2014-11-05 14:47:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('online', updateOnlineStatus);
|
|
|
|
window.addEventListener('offline', updateOnlineStatus);
|
|
|
|
|
|
|
|
updateOnlineStatus();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
```
|