ipcMain
Kommunizieren Sie asynchron vom Hauptprozess zum Rendererprozess.
Process: Main
Das ipcMain -Modul ist ein Event Emitter. Wenn es im Hauptprozess verwendet wird, verarbeitet es asynchrone und synchrone Nachrichten, die von einem Renderer Prozess (Webseite) gesendet werden. Nachrichten, die von einem Renderer gesendet werden, werden an dieses Modul übergegeben.
For usage examples, check out the IPC tutorial.
Sending messages
It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.
- Beim Senden einer Nachricht ist der Ereignisname der
channel. - Um auf eine synchrone Nachricht zu antworten, müssen Sie
event.returnValuefestlegen. - Um eine asynchrone Nachricht an den Absender zurückzusenden, können Sie
event.reply(...)verwenden. Diese Hilfsmethode verarbeitet automatisch Nachrichten, die von Frames kommen, die nicht der Hauptframe sind (z. B. iframes), währendevent.sender.send(...)immer an den Hauptframe sendet.
Methoden
The ipcMain module has the following methods to listen for events:
ipcMain.on(channel, listener)
channelstringlistenerFunktioneventIpcMainEvent...argsany[]
Hört channelab, wenn eine neue Nachricht eintrifft wird listener mit listener(event, args...) aufgerufen.
ipcMain.off(channel, listener)
channelstringlistenerFunktioneventIpcMainEvent...argsany[]
Entfernt den angegebenen listener aus dem Listener-Array für den angegebenen channel.
ipcMain.once(channel, listener)
channelstringlistenerFunktioneventIpcMainEvent...argsany[]
Fügt einen einmaligen listener-Funktion für das Ereignis hinzu. Diese listener wird nur aufgerufen, wenn das nächste Mal eine Nachricht an channelgesendet wird, danach wird sie entfernt.
ipcMain.addListener(channel, listener)
channelstringlistenerFunktioneventIpcMainEvent...argsany[]
Alias for ipcMain.on.
ipcMain.removeListener(channel, listener)
channelstringlistenerFunktion...argsany[]
Alias for ipcMain.off.
ipcMain.removeAllListeners([channel])
channelstring (optional)
Removes all listeners from the specified channel. Removes all listeners from all channels if no channel is specified.
ipcMain.handle(channel, listener)
channelstringlistenerFunction<Promise<any> | any>eventIpcMainInvokeEvent...argsany[]
Fügt einen Handler für einen invokefähigen IPC hinzu. Dieser Handler wird immer dann aufgerufen, wenn ein Renderer ipcRenderer.invoke(channel, ...args) aufruft.
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.
ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
const result = await somePromise(...args)
return result
})
async () => {
const result = await ipcRenderer.invoke('my-invokable-ipc', arg1, arg2)
// ...
}
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.
Errors thrown through handle in the main process are not transparent as they are serialized and only the message property from the original error is provided to the renderer process. Please refer to #24427 for details.
ipcMain.handleOnce(channel, listener)
channelstringlistenerFunction<Promise<any> | any>eventIpcMainInvokeEvent...argsany[]
Handles a single invokeable IPC message, then removes the listener. See ipcMain.handle(channel, listener).
ipcMain.removeHandler(channel)
channelstring
Removes any handler for channel, if present.