ipcRenderer
Communiquer de manière asynchrone depuis le processus renderer au processus main.
Processus : Renderer
Le module ipcRender
est un EventEmitter. Il fournit quelques méthodes pour pouvoir envoyer des messages synchrones et asynchrones depuis le processus render (page web) pour le processus main. Vous pouvez également recevoir des réponses du processus main.
See IPC tutorial for code examples.
Méthodes
Le module de ipcRenderer
possède les méthodes suivantes pour écouter les événements et envoyer des messages :
ipcRenderer.on(channel, listener)
channel
stringlistener
Functionévénement
IpcRendererEvent...args
any[]
En écoutant channel
, lorsqu'un nouveau message arrive, listener
sera appelé comme ceci listener(event, args...)
.
ipcRenderer.once(channel, listener)
channel
stringlistener
Functionévénement
IpcRendererEvent...args
any[]
Ajoute un listener
à déclenchement unique pour l’événement. Ce listener
sera appelé uniquement lors de la prochaine émission d'un message sur le channel
, après quoi il sera supprimé.
ipcRenderer.removeListener(channel, listener)
channel
stringlistener
Function...args
any[]
Supprime le listener
spécifié du tableau d'écouteurs pour le channel
spécifié.
ipcRenderer.removeAllListeners(channel)
channel
string
Supprime tous les écouteurs, ou ceux du channel
spécifié.
ipcRenderer.send(channel, ...args)
channel
string...args
any[]
Envoyez un message asynchrone au processus principal via channel
, ainsi que des arguments. Les arguments seront sérialisés avec le Structured Clone Algorithm, tout comme window.postMessage
, de sorte que les chaînes prototypes ne seront pas incluses. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
NOTE: L'envoi de types Javascript non standards tels que des objets DOM ou des objets spéciaux Electron est déprécié, et déclenchera une exception à partir d'Electron 9.
Since the main process does not have support for DOM objects such as
ImageBitmap
,File
,DOMMatrix
and so on, such objects cannot be sent over Electron's IPC to the main process, as the main process would have no way to decode them. Attempting to send such objects over IPC will result in an error.
Le processus principal le gère en écoutant le canal
avec le module ipcMain
.
If you need to transfer a MessagePort
to the main process, use ipcRenderer.postMessage
.
If you want to receive a single response from the main process, like the result of a method call, consider using ipcRenderer.invoke
.
ipcRenderer.invoke(channel, ...args)
channel
string...args
any[]
Retourne Promise<any>
- résout avec la réponse du processus principal.
Envoie un message au processus principal via channel
et attend un résultat asynchrone. Les arguments seront sérialisés avec le Structured Clone Algorithm, tout comme window.postMessage
, de sorte que les chaînes prototypes ne seront pas incluses. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
NOTE: L'envoi de types Javascript non standards tels que des objets DOM ou des objets spéciaux Electron est déprécié, et déclenchera une exception à partir d'Electron 9.
Since the main process does not have support for DOM objects such as
ImageBitmap
,File
,DOMMatrix
and so on, such objects cannot be sent over Electron's IPC to the main process, as the main process would have no way to decode them. Attempting to send such objects over IPC will result in an error.
Le processus principal devrait écouter le canal
avec ipcMain.handle()
.
Par exemple :
// Processus de rendu
ipcRenderer.invoke('some-name', someArgument).then((result) => {
// ...
})
// Processus principal
ipcMain.handle('some-name', async (event, someArgument) => {
const result = wait doSomeWork(someArgument)
return result
})
If you need to transfer a MessagePort
to the main process, use ipcRenderer.postMessage
.
If you do not need a response to the message, consider using ipcRenderer.send
.
ipcRenderer.sendSync(canal, ...args)
channel
string...args
any[]
Retourne any
- La valeur renvoyé par l'écouteur du ipcMain
.
Envoyez un message au processus principal via channel
et attendez un résultat de manière synchrone. Les arguments seront sérialisés avec le Structured Clone Algorithm, tout comme window.postMessage
, de sorte que les chaînes prototypes ne seront pas incluses. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
NOTE: L'envoi de types Javascript non standards tels que des objets DOM ou des objets spéciaux Electron est déprécié, et déclenchera une exception à partir d'Electron 9.
Since the main process does not have support for DOM objects such as
ImageBitmap
,File
,DOMMatrix
and so on, such objects cannot be sent over Electron's IPC to the main process, as the main process would have no way to decode them. Attempting to send such objects over IPC will result in an error.
Le processus principal le gère en écoutant le canal
avec le module ipcMain
, et répond en définissant event.returnValue
.
⚠️ WARNING: Sending a synchronous message will block the whole renderer process until the reply is received, so use this method only as a last resort. It's much better to use the asynchronous version,
invoke()
.
ipcRenderer.postMessage(channel, message, [transfer])
channel
stringmessage
toustransfer
MessagePort[] (optional)
Send a message to the main process, optionally transferring ownership of zero or more MessagePort
objects.
The transferred MessagePort
objects will be available in the main process as MessagePortMain
objects by accessing the ports
property of the emitted event.
Par exemple :
// Renderer process
const { port1, port2 } = new MessageChannel()
ipcRenderer.postMessage('port', { message: 'hello' }, [port1])
// Main process
ipcMain.on('port', (e, msg) => {
const [port] = e.ports
// ...
})
For more information on using MessagePort
and MessageChannel
, see the MDN documentation.
ipcRenderer.sendTo(webContentsId, channel, ...args)
webContentsId
numberchannel
string...args
any[]
Envoie un message à une fenêtre avec webContentsId
via canal
.
ipcRenderer.sendToHost(canal, ...args)
channel
string...args
any[]
Comme ipcRenderer.send
, mais l'événement sera envoyé à l'élément <webview>
dans la page hôte au lieu du processus main.
Objet event
La documentation de l'objet événement
passé à la callback
peut être trouvée dans la documentation de la structure ipc-renderer-event
.