ipcRenderer

从渲染器进程到主进程的异步通信。

进程: Renderer

The ipcRenderer module is an EventEmitter. 你可以使用它提供的一些方法从渲染进程 (web 页面) 发送同步或异步的消息到主进程。 也可以接收主进程回复的消息。

请从 ipcMain 查看代码示例。

方法

ipcRenderer 模块使用以下方法来监听事件和发送消息。

ipcRenderer.on(channel, listener)

  • channel String
  • listener Function
    • event IpcRendererEvent
    • ...args any[]

监听 channel, 当新消息到达,将通过 listener(event, args…) 调用 listener。

ipcRenderer.once(channel, listener)

  • channel String
  • listener Function - 回调函数
    • event IpcRendererEvent
    • ...args any[]

为事件添加一个一次性用的listener 函数.这个 listener 只有在下次的消息到达 channel 时被请求调用,之后就被删除了.

ipcRenderer.removeListener(channel, listener)

  • channel String
  • listener Function
    • ...args any[]

为特定的 channel 从监听队列中删除特定的 listener 监听者.

ipcRenderer.removeAllListeners(channel)

  • channel String

移除所有的监听器,当指定 channel 时只移除与其相关的所有监听器。

ipcRenderer.send(channel, ...args)

  • channel String
  • ...args any[]

Send an asynchronous message to the main process via channel, along with arguments. Arguments will be serialized with the Structured Clone Algorithm, just like [postMessage][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.

NOTE: Sending non-standard JavaScript types such as DOM objects or special Electron objects is deprecated, and will begin throwing an exception starting with Electron 9.

The main process handles it by listening for channel with the ipcMain module.

ipcRenderer.invoke(channel, ...args)

  • channel String
  • ...args any[]

Returns Promise<any> - Resolves with the response from the main process.

Send a message to the main process via channel and expect a result asynchronously. Arguments will be serialized with the Structured Clone Algorithm, just like [postMessage][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.

NOTE: Sending non-standard JavaScript types such as DOM objects or special Electron objects is deprecated, and will begin throwing an exception starting with Electron 9.

The main process should listen for channel with ipcMain.handle().

例如:

  1. // Renderer process
  2. ipcRenderer.invoke('some-name', someArgument).then((result) => {
  3. // ...
  4. })
  5. // Main process
  6. ipcMain.handle('some-name', async (event, someArgument) => {
  7. const result = await doSomeWork(someArgument)
  8. return result
  9. })

ipcRenderer.sendSync(channel, ...args)

  • channel String
  • ...args any[]

返回 any - 由 ipcMain 处理程序发送过来的值。

Send a message to the main process via channel and expect a result synchronously. Arguments will be serialized with the Structured Clone Algorithm, just like [postMessage][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.

NOTE: Sending non-standard JavaScript types such as DOM objects or special Electron objects is deprecated, and will begin throwing an exception starting with Electron 9.

主进程可以使用 ipcMain 监听 channel来接收这些消息,并通过 event.returnValue设置回复消息。

:warning: WARNING: Sending a synchronous message will block the whole renderer process until the reply is received, so use this method only as a last resort. It’s much better to use the asynchronous version, invoke().

ipcRenderer.sendTo(webContentsId, channel, ...args)

  • webContentsId Number
  • channel String
  • ...args any[]

Sends a message to a window with webContentsId via channel.

ipcRenderer.sendToHost(channel, ...args)

  • channel String
  • ...args any[]

就像 ipcRenderer.send,不同的是消息会被发送到 host 页面上的 <webview> 元素,而不是主进程。

事件对象

The documentation for the event object passed to the callback can be found in the ipc-renderer-event structure docs.