ipcMain
进程:主进程
ipcRenderer
是一个 的实例。 当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息。 从渲染器进程发送的消息将被发送到该模块。
也可以从主进程向渲染进程发送消息,查阅ebContents.send获取更多信息。
- 回复同步信息时,需要设置
event.returnValue
。 - 可以使用
event.sender.send(...)
将异步消息发送回发送者。 This helper method will automatically handle messages coming from frames that aren’t the main frame (e.g. iframes) whereasevent.sender.send(...)
will always send to the main frame.
下面是在渲染和主进程之间发送和处理消息的一个例子:
channel
Stringlistener
Functionevent
IpcMainEvent...args
any[]
监听 channel
,当接收到新的消息时 listener
会以 listener(event, args...)
的形式被调用。
channel
Stringlistener
Functionevent
IpcMainEvent...args
any[]
Adds a one time listener
function for the event. This listener
is invoked only the next time a message is sent to channel
, after which it is removed.
channel
String
从监听器数组中移除监听 channel
的指定 listener
。
ipcMain.removeAllListeners([channel])
channel
String (optional)
删除所有监听者,或特指的 channel 的所有监听者.
channel
Stringlistener
Function| any> event
IpcMainInvokeEvent- any[]
If listener
returns a Promise, the eventual result of the promise will be returned as a reply to the remote caller. Otherwise, the return value of the listener will be used as the value of the reply.
The event
that is passed as the first argument to the handler is the same as that passed to a regular event listener. It includes information about which WebContents is the source of the invoke request.
ipcMain.handleOnce(channel, listener)
channel
Stringlistener
Function| any> event
IpcMainInvokeEvent...args
any[]
Handles a single invoke
able IPC message, then removes the listener. See ipcMain.handle(channel, listener)
.
channel
String
Removes any handler for channel
, if present.
The documentation for the event
object passed to handle
callbacks can be found in the structure docs.