зеркало из https://github.com/electron/electron.git
docs: use webContents.mainFrame.on() in MessagePort tutorial (#35824)
* docs: use webContents.mainFrame.on() in MessagePort tutorial * Update docs/tutorial/message-ports.md Co-authored-by: Samuel Maddock <smaddock@salesforce.com> Co-authored-by: Samuel Maddock <smaddock@salesforce.com>
This commit is contained in:
Родитель
76afd8c028
Коммит
1328d8d670
|
@ -180,19 +180,16 @@ app.whenReady().then(async () => {
|
|||
|
||||
// We can't use ipcMain.handle() here, because the reply needs to transfer a
|
||||
// MessagePort.
|
||||
ipcMain.on('request-worker-channel', (event) => {
|
||||
// For security reasons, let's make sure only the frames we expect can
|
||||
// access the worker.
|
||||
if (event.senderFrame === mainWindow.webContents.mainFrame) {
|
||||
// Create a new channel ...
|
||||
const { port1, port2 } = new MessageChannelMain()
|
||||
// ... send one end to the worker ...
|
||||
worker.webContents.postMessage('new-client', null, [port1])
|
||||
// ... and the other end to the main window.
|
||||
event.senderFrame.postMessage('provide-worker-channel', null, [port2])
|
||||
// Now the main window and the worker can communicate with each other
|
||||
// without going through the main process!
|
||||
}
|
||||
// Listen for message sent from the top-level frame
|
||||
mainWindow.webContents.mainFrame.on('request-worker-channel', (event) => {
|
||||
// Create a new channel ...
|
||||
const { port1, port2 } = new MessageChannelMain()
|
||||
// ... send one end to the worker ...
|
||||
worker.webContents.postMessage('new-client', null, [port1])
|
||||
// ... and the other end to the main window.
|
||||
event.senderFrame.postMessage('provide-worker-channel', null, [port2])
|
||||
// Now the main window and the worker can communicate with each other
|
||||
// without going through the main process!
|
||||
})
|
||||
})
|
||||
```
|
||||
|
|
Загрузка…
Ссылка в новой задаче