Skip to main content

ipcMain

Communicate asynchronously from the main process to renderer processes.从主进程到渲染器进程进行异步通信。

Process:进程:Main

The ipcMain module is an Event Emitter. ipcMain模块是一个事件发射器。When used in the main process, it handles asynchronous and synchronous messages sent from a renderer process (web page). 当在主进程中使用时,它处理从呈现器进程(网页)发送的异步和同步消息。Messages sent from a renderer will be emitted to this module.从渲染器发送的消息将被发送到此模块。

For usage examples, check out the IPC tutorial.有关用法示例,请查看IPC教程

Sending messages发送消息

It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.也可以将消息从主进程发送到渲染器进程,有关更多信息,请参阅webContents.send

  • When sending a message, the event name is the channel.发送消息时,事件名称为channel
  • To reply to a synchronous message, you need to set event.returnValue.要回复同步消息,您需要设置event.returnValue
  • To send an asynchronous message back to the sender, you can use event.reply(...). 要将异步消息发送回发送方,可以使用event.reply(...)This helper method will automatically handle messages coming from frames that aren't the main frame (e.g. iframes) whereas event.sender.send(...) will always send to the main frame.这个helper方法将自动处理来自非主框架的帧(例如iframes)的消息,而event.sender.send(...)将始终发送到主框架。

Methods方法

The ipcMain module has the following method to listen for events:ipcMain模块具有以下方法来侦听事件:

ipcMain.on(channel, listener)

  • channel string
  • listener Function

Listens to channel, when a new message arrives listener would be called with listener(event, args...).侦听channel,当新消息到达时,listener将与listener(event, args...)一起调用。

ipcMain.once(channel, listener)

  • channel string
  • listener Function

Adds a one time listener function for the event. 为事件添加一个一次性listener函数。This listener is invoked only the next time a message is sent to channel, after which it is removed.只有在下一次将消息发送到channel时才会调用此listener,然后将其删除。

ipcMain.removeListener(channel, listener)

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

Removes the specified listener from the listener array for the specified channel.从指定通道的listener数组中删除指定的channel

ipcMain.removeAllListeners([channel])

  • channel string (optional)

Removes listeners of the specified channel.删除指定频道的channel

ipcMain.handle(channel, listener)

  • channel string
  • listener Function<Promise <void> | any>

Adds a handler for an invokeable IPC. 为可调用的的IPC添加一个处理程序。This handler will be called whenever a renderer calls ipcRenderer.invoke(channel, ...args).每当渲染器调用ipcRenderer.invoke(channel, ...args)时,都会调用此处理程序。

If listener returns a Promise, the eventual result of the promise will be returned as a reply to the remote caller. 如果listener返回Promise,那么Promise的最终结果将作为对远程调用者的答复返回。Otherwise, the return value of the listener will be used as the value of the reply.否则,侦听器的返回值将用作答复的值。

Main Process主要流程
ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
const result = await somePromise(...args)
return result
})
Renderer Process
async () => {
const result = await ipcRenderer.invoke('my-invokable-ipc', arg1, arg2)
// ...
}

The event that is passed as the first argument to the handler is the same as that passed to a regular event listener. 作为第一个参数传递给处理程序的事件与传递给常规事件侦听器的event相同。It includes information about which WebContents is the source of the invoke request.它包括有关哪个WebContents是调用请求的源的信息。

Errors thrown through handle in the main process are not transparent as they are serialized and only the message property from the original error is provided to the renderer process. 通过主进程中的handle抛出的错误是不透明的,因为它们被序列化,并且只有原始错误的消息属性被提供给呈现器进程。Please refer to #24427 for details.有关详细信息,请参阅#24427

ipcMain.handleOnce(channel, listener)

  • channel string
  • listener Function<Promise <void> | any>

Handles a single invokeable IPC message, then removes the listener. 处理单个可调用的IPC消息,然后删除侦听器。See ipcMain.handle(channel, listener).请参阅ipcMain.handle(channel, listener)

ipcMain.removeHandler(channel)

  • channel string

Removes any handler for channel, if present.删除channel的任何处理程序(如果存在)。

IpcMainEvent object

The documentation for the event object passed to the callback can be found in the ipc-main-event structure docs.传递给callbackevent对象的文档可以在ipc-main-event结构文档中找到。

IpcMainInvokeEvent object

The documentation for the event object passed to handle callbacks can be found in the ipc-main-invoke-event structure docs.传递给handle回调的event对象的文档可以在ipc-main-invoke-event结构文档中找到。