2015-09-02 12:12:22 +00:00
|
|
|
|
# 在线/离线事件探测
|
2017-01-17 09:50:16 +00:00
|
|
|
|
|
2015-09-02 12:12:22 +00:00
|
|
|
|
使用标准 HTML5 APIs 可以实现在线和离线事件的探测,就像以下例子:
|
|
|
|
|
|
2016-10-03 23:40:51 +00:00
|
|
|
|
_main.js_
|
|
|
|
|
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```javascript
|
2016-10-03 23:40:51 +00:00
|
|
|
|
const {app, BrowserWindow} = require('electron')
|
|
|
|
|
|
|
|
|
|
let onlineStatusWindow
|
2015-09-02 12:12:22 +00:00
|
|
|
|
|
2016-10-03 23:40:51 +00:00
|
|
|
|
app.on('ready', () => {
|
2016-10-03 03:47:16 +00:00
|
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
|
2016-10-03 22:48:04 +00:00
|
|
|
|
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
|
2016-10-03 03:47:16 +00:00
|
|
|
|
})
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```
|
2015-09-02 12:12:22 +00:00
|
|
|
|
|
2016-10-03 23:40:51 +00:00
|
|
|
|
_online-status.html_
|
|
|
|
|
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```html
|
2015-09-02 12:12:22 +00:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
2016-10-03 23:40:51 +00:00
|
|
|
|
<body>
|
|
|
|
|
<script>
|
|
|
|
|
const alertOnlineStatus = () => {
|
|
|
|
|
window.alert(navigator.onLine ? 'online' : 'offline')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('online', alertOnlineStatus)
|
|
|
|
|
window.addEventListener('offline', alertOnlineStatus)
|
|
|
|
|
|
|
|
|
|
alertOnlineStatus()
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
2015-09-02 12:12:22 +00:00
|
|
|
|
</html>
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```
|
2015-09-02 12:12:22 +00:00
|
|
|
|
|
2016-06-15 11:30:02 +00:00
|
|
|
|
也会有人想要在主进程也有回应这些事件的实例。然后主进程没有 `navigator` 对象因此不能直接探测在线还是离线。使用 Electron 的进程间通讯工具,事件就可以在主进程被使用,就像下面的例子:
|
2015-09-02 12:12:22 +00:00
|
|
|
|
|
|
|
|
|
*main.js*
|
2017-01-17 09:50:16 +00:00
|
|
|
|
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```javascript
|
2017-01-17 09:50:16 +00:00
|
|
|
|
const {app, BrowserWindow, ipcMain} = require('electron')
|
|
|
|
|
let onlineStatusWindow
|
2015-09-02 12:12:22 +00:00
|
|
|
|
|
2017-01-17 09:50:16 +00:00
|
|
|
|
app.on('ready', () => {
|
2016-10-03 03:47:16 +00:00
|
|
|
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
|
2016-10-03 22:48:04 +00:00
|
|
|
|
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
|
2016-10-03 03:47:16 +00:00
|
|
|
|
})
|
2015-09-02 12:12:22 +00:00
|
|
|
|
|
2017-01-17 09:50:16 +00:00
|
|
|
|
ipcMain.on('online-status-changed', (event, status) => {
|
2016-10-03 03:47:16 +00:00
|
|
|
|
console.log(status)
|
|
|
|
|
})
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```
|
2015-09-02 12:12:22 +00:00
|
|
|
|
|
|
|
|
|
*online-status.html*
|
2017-01-17 09:50:16 +00:00
|
|
|
|
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```html
|
2015-09-02 12:12:22 +00:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
2017-01-17 09:50:16 +00:00
|
|
|
|
<body>
|
|
|
|
|
<script>
|
|
|
|
|
const {ipcRenderer} = require('electron')
|
|
|
|
|
const updateOnlineStatus = () => {
|
|
|
|
|
ipcRenderer.send('online-status-changed', navigator.onLine ? 'online' : 'offline')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('online', updateOnlineStatus)
|
|
|
|
|
window.addEventListener('offline', updateOnlineStatus)
|
|
|
|
|
|
|
|
|
|
updateOnlineStatus()
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
2015-09-02 12:12:22 +00:00
|
|
|
|
</html>
|
2015-12-19 06:27:47 +00:00
|
|
|
|
```
|
2017-01-17 09:50:16 +00:00
|
|
|
|
|
|
|
|
|
**注意:** 如果 Electron 无法连接到局域网(LAN)或
|
|
|
|
|
一个路由器,它被认为是离线的; 所有其他条件返回 `true`。
|
|
|
|
|
所以,虽然你可以假设 Electron 是离线的,当 `navigator.onLine`
|
|
|
|
|
返回一个 `false' 值,你不能假设一个 'true' 值必然
|
|
|
|
|
意味着 Electron 可以访问互联网。你可能会获得虚假
|
|
|
|
|
连接,例如在计算机运行虚拟化软件的情况下,
|
|
|
|
|
具有始终“连接”的虚拟以太网适配器。
|
|
|
|
|
因此,如果您真的想确定 Electron 的互联网访问状态,
|
|
|
|
|
你应该开发额外的检查手段。
|