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:
Milan Burda 2022-10-12 16:27:58 +02:00 коммит произвёл GitHub
Родитель 76afd8c028
Коммит 1328d8d670
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 10 добавлений и 13 удалений

Просмотреть файл

@ -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!
})
})
```