ipcMain
Comunica de forma assíncrona o processo principal aos processos de renderização.
Process: Main
O módulo ipcMain é um emissor de eventos. Quando usado no processo principal, ele lida com mensagens assíncronas e síncronas enviadas a partir de um processo de renderização (página da web). As mensagens enviadas de um renderizador serão emitidas para este módulo.
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.
- Ao enviar uma mensagem, o nome do evento é o
channel. - Para responder a uma mensagem síncrona, você precisa de configurar
event.returnValue. - Para enviar uma mensagem assíncrona de volta para o remetente, você pode usar
event.reply(...). This helper method will automatically handle messages coming from frames that aren't the main frame (e.g. iframes) whereasevent.sender.send(...)will always send to the main frame.
Métodos
The ipcMain module has the following methods to listen for events:
ipcMain.on(channel, listener)
channelstringlistenerFunçãoeventIpcMainEvent...argsany[]
Ouve o channel, quando uma mensagem chega, o listener deve ser chamado com listener(event, args...).
ipcMain.off(channel, listener)
channelstringlistenerFunçãoeventIpcMainEvent...argsany[]
Removes the specified listener from the listener array for the specified channel.
ipcMain.once(channel, listener)
channelstringlistenerFunçãoeventIpcMainEvent...argsany[]
Adiciona uma função listener única para o evento. This listener is invoked only the next time a message is sent to channel, after which it is removed.
ipcMain.addListener(channel, listener)
channelstringlistenerFunçãoeventIpcMainEvent...argsany[]
Alias for ipcMain.on.
ipcMain.removeListener(channel, listener)
channelstringlistenerFunção...argsany[]
Alias for ipcMain.off.
ipcMain.removeAllListeners([channel])
channelstring (opcional)
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[]
Adds a handler for an invokeable IPC. This handler will be called whenever a renderer calls ipcRenderer.invoke(channel, ...args).
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.