> For the complete documentation index, see [llms.txt](https://yuzhigang5460.gitbook.io/electron/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yuzhigang5460.gitbook.io/electron/tutorial/online-offline-events.md).

# 在线/离线事件检测

在渲染器进程中,使用标准HTML5 API实现在线和离线事件检测,例子:

*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*

```markup
<!DOCTYPE html>
<html>
<body>
<script>
  const alertOnlineStatus = () => {
    window.alert(navigator.onLine ? 'online' : 'offline')
  }

  window.addEventListener('online',  alertOnlineStatus)
  window.addEventListener('offline',  alertOnlineStatus)

  alertOnlineStatus()
</script>
</body>
</html>
```

你也许会希望在主程序中回应这些事件,但由于主进程中不存在`navigator`对象，所以无法直接检测在线还是离线。

不过,你可以使用Electron的进程间通信方法将事件进行转发到主进程进行处理，例子:

*main.js*

```javascript
const {app, BrowserWindow, ipcMain} = require('electron')
let onlineStatusWindow
//ready时启用监听
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*

```markup
<!DOCTYPE html>
<html>
<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>
</html>
```

**注意:** 除了Electron未连接局域网(LAN)或路由器被视为离线(脱机)之外,其他条件都返回 `true`。

尽管如此,当 `navigator.onLine` 返回一个 `false`值时,你不能想当然的以为 `true`即已联网,因为联网状态可以轻易伪造,比如计算机使用虚拟化软件始终连接着虚拟以太网网络适配器.

所以,如果要确定 Electron的真实联网状态，您应该使用更多检查手段。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://yuzhigang5460.gitbook.io/electron/tutorial/online-offline-events.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
