ipcRenderer模块是一个 EventEmitter类的实例. 它提供了有限的方法,你可以从渲染进程向主进程发送同步或异步消息. 也可以收到主进程的响应.
查看 ipcMain 代码例子.
ipcRenderer模块有下列方法来监听事件:
ipcRenderer.on(channel, listener)channelStringlistenerFunction监听 channel, 当有新消息到达,使用 listener(event, args...)调用 listener.
ipcRenderer.once(channel, listener)channelStringlistenerFunction为这个事件添加一个一次性 listener函数.这个 listener将在下一次有新消息被发送到 channel的时候被请求调用,之后就被删除了.
ipcRenderer.removeListener(channel, listener)channelStringlistenerFunction从指定的 channel中的监听者数组删除指定的 listener.
ipcRenderer.removeAllListeners([channel])channelString (optional)删除所有的监听者,或者删除指定 channel中的全部.
ipcRenderer模块有如下方法来发送消息:
ipcRenderer.send(channel[, arg1][, arg2][, ...])channelStringarg(可选)通过 channel向主进程发送异步消息,也可以发送任意参数.参数会被JSON序列化,之后就不会包含函数或原型链.
主进程通过使用 ipcMain模块来监听 channel,从而处理消息.
ipcRenderer.sendSync(channel[, arg1][, arg2][, ...])channelStringarg(可选)通过 channel向主进程发送同步消息,也可以发送任意参数.参数会被JSON序列化,之后就不会包含函数或原型链.
主进程通过使用 ipcMain模块来监听 channel,从而处理消息, 通过 event.returnValue来响应.
注意: 发送同步消息将会阻塞整个渲染进程,除非你知道你在做什么,否则就永远不要用它 .
ipcRenderer.sendToHost(channel[, arg1][, arg2][, ...])channelStringarg(可选)类似 ipcRenderer.send,但是它的事件将发往 host page 的 <webview>元素,而不是主进程.