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这个helper方法将自动处理来自非主框架的帧(例如iframes)的消息,而event.sender.send(...)
will always send to the main frame.event.sender.send(...)
将始终发送到主框架。
Methods方法
The ipcMain
module has the following method to listen for events:ipcMain
模块具有以下方法来侦听事件:
ipcMain.on(channel, listener)
channel
stringlistener
Functionevent
IpcMainEvent...args
any[]
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
stringlistener
Functionevent
IpcMainEvent...args
any[]
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
stringlistener
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
stringlistener
Function<Promise <void> | any>event
IpcMainInvokeEvent...args
any[]
Adds a handler for an 为可调用的的IPC添加一个处理程序。invokeable
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.否则,侦听器的返回值将用作答复的值。
ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
const result = await somePromise(...args)
return result
})
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
stringlistener
Function<Promise <void> | any>event
IpcMainInvokeEvent...args
any[]
Handles a single 处理单个可调用的IPC消息,然后删除侦听器。invoke
able IPC message, then removes the listener. 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.callback
的event
对象的文档可以在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结构文档中找到。