ipcRenderer

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

进程: 渲染进程

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

请从 ipcMain 查看代码示例。

方法

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

ipcRenderer.on(channel, listener)

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

监听 channel,当接收到新的消息时 listener 会以 listener(event, args...) 的形式被调用。

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[]

通过channel向主进程发送异步消息,可以发送任意参数。 Arguments will be serialized with the Structured Clone Algorithm, just like window.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 will throw an exception.

Since the main process does not have support for DOM objects such as ImageBitmap, File, DOMMatrix and so on, such objects cannot be sent over Electron’s IPC to the main process, as the main process would have no way to decode them. Attempting to send such objects over IPC will result in an error.

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

If you need to transfer a MessagePort to the main process, use ipcRenderer.postMessage.

If you want to receive a single response from the main process, like the result of a method call, consider using ipcRenderer.invoke.

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 window.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 will throw an exception.

Since the main process does not have support for DOM objects such as ImageBitmap, File, DOMMatrix and so on, such objects cannot be sent over Electron’s IPC to the main process, as the main process would have no way to decode them. Attempting to send such objects over IPC will result in an error.

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. })

If you need to transfer a MessagePort to the main process, use ipcRenderer.postMessage.

如果你不需要回复此消息,请考虑使用 ipcRender.sent

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 window.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 will throw an exception.

Since the main process does not have support for DOM objects such as ImageBitmap, File, DOMMatrix and so on, such objects cannot be sent over Electron’s IPC to the main process, as the main process would have no way to decode them. Attempting to send such objects over IPC will result in an error.

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

⚠️ 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.postMessage(channel, message, [transfer])

  • channel String
  • message any
  • transfer MessagePort

Send a message to the main process, optionally transferring ownership of zero or more MessagePort objects.

The transferred MessagePort objects will be available in the main process as MessagePortMain objects by accessing the ports property of the emitted event.

例如:

  1. // Renderer process
  2. const { port1, port2 } = new MessageChannel()
  3. ipcRenderer.postMessage('port', { message: 'hello' }, [port1])
  4. // Main process
  5. ipcMain.on('port', (e, msg) => {
  6. const [port] = e.ports
  7. // ...
  8. })

For more information on using MessagePort and MessageChannel, see the MDN documentation.

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.