]> khamidou — GitHub mirrors - electron.git/commitdiff
Add tutorial on online/offline event detection
authorBrent Ertz <brent.ertz@gmail.com>
Wed, 5 Nov 2014 14:47:38 +0000 (07:47 -0700)
committerBrent Ertz <brent.ertz@gmail.com>
Wed, 5 Nov 2014 14:49:50 +0000 (07:49 -0700)
docs/README.md
docs/tutorial/online-offline-events.md [new file with mode: 0644]

index 9c8cc152612b1948a0ea0dab9fc7bb2bc2d07787..d22ef38e72386346df092e4b5a3d029b96d6f1b8 100644 (file)
@@ -7,6 +7,7 @@
 * [Debugging browser process](tutorial/debugging-browser-process.md)
 * [Using Selenium and WebDriver](tutorial/using-selenium-and-webdriver.md)
 * [DevTools extension](tutorial/devtools-extension.md)
+* [Online/offline event detection](tutorial/online-offline-events.md)
 
 ## API references
 
diff --git a/docs/tutorial/online-offline-events.md b/docs/tutorial/online-offline-events.md
new file mode 100644 (file)
index 0000000..825296d
--- /dev/null
@@ -0,0 +1,80 @@
+# 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 });
+  onlineStatusWindow.loadUrl('file://' + path.join(__dirname, '/online-status.html'));
+});
+```
+
+_online-status.html_
+
+```html
+<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
+browser process as well.  The browser process however does not have a
+"navigator" object and thus cannot detect these events directly.  Using
+Atom-shell's inter-process communication utilities, the events can be forwarded
+to the browser process and handled as needed, as shown in the following example.
+
+_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 });
+  onlineStatusWindow.loadUrl('file://' + path.join(__dirname, '/online-status.html'));
+});
+
+ipc.on('onlineStatusMessage', function(event, status) {
+  console.log(status);
+});
+```
+
+_online-status.html_
+
+```html
+<html>
+  <body>
+    <script>
+      var ipc = require('ipc');
+      var updateOnlineStatus = function() {
+        ipc.send('onlineStatusMessage', navigator.onLine ? 'online' : 'offline');
+      };
+
+      window.addEventListener('online',  updateOnlineStatus);
+      window.addEventListener('offline',  updateOnlineStatus);
+
+      updateOnlineStatus();
+    </script>
+  </body>
+</html>
+```