ipcRenderer
进程: Renderer
ipcRenderer
是一个 的实例。 你可以使用它提供的一些方法从渲染进程 (web 页面) 发送同步或异步的消息到主进程。 也可以接收主进程回复的消息。
请从 ipcMain 查看代码示例。
ipcRenderer
模块使用以下方法来监听事件和发送消息。
channel
Stringlistener
Functionevent
IpcRendererEvent...args
any[]
监听 channel, 当新消息到达,将通过 listener(event, args…) 调用 listener。
ipcRenderer.once(channel, listener)
channel
Stringlistener
Function - 回调函数event
IpcRendererEvent...args
any[]
为事件添加一个一次性用的listener 函数.这个 listener 只有在下次的消息到达 channel 时被请求调用,之后就被删除了.
ipcRenderer.removeListener(channel, listener)
- String
listener
Function
为特定的 channel 从监听队列中删除特定的 listener 监听者.
channel
String
ipcRenderer.send(channel, ...args)
channel
String...args
any[]
通过channel
向渲染器进程发送异步消息,可以发送任意参数。 Arguments will be serialized with the Structured Clone Algorithm, just like [postMessage
][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
The main process handles it by listening for channel
with the module.
ipcRenderer.invoke(channel, ...args)
channel
String...args
any[]
Returns Promise<any>
- Resolves with the response from the main process.
Send a message to the main process via channel
and expect a result asynchronously. Arguments will be serialized with the , just like [postMessage
][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
The main process should listen for channel
with ipcMain.handle()
.
- String
...args
any[]
返回 any
- 由 处理程序发送过来的值。
Send a message to the main process via channel
and expect a result synchronously. Arguments will be serialized with the Structured Clone Algorithm, just like [postMessage
][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
主进程可以使用 ipcMain
监听 来接收这些消息,并通过 event.returnValue
设置回复消息。
ipcRenderer.sendTo(webContentsId, channel, ...args)
webContentsId
Numberchannel
String...args
any[]
Sends a message to a window with webContentsId
via channel
.
ipcRenderer.sendToHost(channel, ...args)
...args
any[]
就像 ipcRenderer.send
,不同的是消息会被发送到 host 页面上的 <webview>
元素,而不是主进程。