- ipcMain
- 发送消息
- 方法
- ipcMain.on(channel, listener)
- ipcMain.once(channel, listener)
- ipcMain.removeListener(channel, listener)
- ipcMain.removeAllListeners([channel])
- 事件对象
- event.returnValue
- event.sender
ipcMain
从主进程到渲染进程的异步通信。
线程:主线程
ipcMain模块是EventEmitter类的一个实例。 当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息。 从渲染器进程发送的消息将被发送到该模块。
发送消息
It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.
- 发送消息时,事件名称为
channel。 - 回复同步信息时,需要设置
event.returnValue。 - 将异步消息发送回发件人,需要使用
event.sender.send(…)。
下面是在渲染和主进程之间发送和处理消息的一个例子:
// 在主进程中.const { ipcMain } = require('electron')ipcMain.on('asynchronous-message', (event, arg) => {console.log(arg) // prints "ping"event.sender.send('asynchronous-reply', 'pong')})ipcMain.on('synchronous-message', (event, arg) => {console.log(arg) // prints "ping"event.returnValue = 'pong'})
//在渲染器进程 (网页) 中。const { ipcRenderer } = require('electron')console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"ipcRenderer.on('asynchronous-reply', (event, arg) => {console.log(arg) // prints "pong"})ipcRenderer.send('asynchronous-message', 'ping')
方法
IpcMain模块有以下方法来侦听事件:
ipcMain.on(channel, listener)
channelStringlistenerFunction
监听channel,当接收到新的消息时listener会以listener(event, args…)的形式被调用。
ipcMain.once(channel, listener)
channelStringlistenerFunction
添加一次性的listener。当且仅当下一个消息发送到channel时listener才会被调用,随后 <0>listener</0> 会被移除。
ipcMain.removeListener(channel, listener)
channelStringlistenerFunction
从监听器数组中移除监听channel的指定listener。
ipcMain.removeAllListeners([channel])
channelString
删除所有监听者,或特指的 channel 的所有监听者.
事件对象
传递给 callback 的 event 对象有如下方法:
event.returnValue
将此设置为在一个同步消息中返回的值.
event.sender
Returns the webContents that sent the message, you can call event.sender.send to reply to the asynchronous message, see webContents.send for more information.
