从主进程到渲染进程的异步通信
Last updated
Was this helpful?
Last updated
Was this helpful?
在主进程中处理由渲染进程发起的异步通信.
进程:
ipcMain
模块是类类的一个实例.
浅显的打个比方,渲染进程给主进程挂个号,这里就开始忙活起来.当然,你也可以从主进程中向渲染进程发送消息.
如果从主进程向渲染进程发送消息,请查看
发送消息,事件名为 channel
.
回应同步消息, 请设置 event.returnValue
.
回应异步消息, 请使用 event.sender.send(...)
.
在主进程和渲染进程之间发送和处理消息的例子:
ipcMain.on(channel, listener)
用途:监听
channel
,并调用listener(event, args...)
处理新消息
channel
String
listener
Function
ipcMain.once(channel, listener)
用途:一次性监听
channel
,当调用listener(event, args...)
处理新消息后删除监听
channel
String
listener
Function - 一次性的 listener
.
ipcMain.removeListener(channel, listener)
用途:从指定
channel
的监听数组中删除特定的listener
channel
String
listener
Function
ipcMain.removeAllListeners([channel])
用途:删除所有监听,或指定
channel
的所有监听
channel
String (可选)
传递给 callback
的 event
对象有如下方法:
event.returnValue
将此设置成同步消息中返回的值.
event.sender
返回发送消息的 webContents
,你可以调用 event.sender.send
来回复异步消息,详见[webContents.send][web-contents-send].