0043. 主进程通过 BrowserWindow 实例的 webContents.send 方法主动给指定的渲染进程发消息
1. 📝 概述
- 主进程 -> 渲染进程
- 主进程:
webContents.send
- 渲染进程:
ipcRenderer.on
- 主进程:
2. 💻 demos.1 - 主进程通过 BrowserWindow 实例的 webContents.send
方法主动给指定的渲染进程发消息
js
const { ipcRenderer } = require('electron')
ipcRenderer.on('msg-from-main-process', (_, ...args) => {
console.log('renderer-process-received-msg-from-main-process')
console.log(args)
})
1
2
3
4
5
2
3
4
5
js
const { app, BrowserWindow, ipcMain } = require('electron')
let win
function createWindow() {
win = new BrowserWindow({
webPreferences: { nodeIntegration: true, contextIsolation: false },
})
win.webContents.openDevTools()
win.loadFile('./index.html')
}
function handleIPC() {
win.webContents.send('msg-from-main-process', 1, 2, 3)
}
app.on('ready', () => {
createWindow()
handleIPC()
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- 最终效果
- 在主进程中找到需要与之通信的那个渲染进程(可以理解为 BrowserWindow 实例),通过 BrowserWindow 实例的
webContents.send
方法主动给指定的渲染进程发消息。实现从主进程到渲染进程的单向通信。
- 在主进程中找到需要与之通信的那个渲染进程(可以理解为 BrowserWindow 实例),通过 BrowserWindow 实例的