ipcMain:当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息,当然也有可能从主进程向渲染进程发送消息。
ipcRenderer: 使用它提供的一些方法从渲染进程 (web 页面) 发送同步或异步的消息到主进程。 也可以接收主进程回复的消息。
场景 1:渲染进程给主进程发送异步消息:
//渲染进程
const { ipcRenderer } = require('electron');
ipcRenderer.send('msg',{name:'张三'}); //异步//主进程
const { ipcMain } = require('electron');
ipcMain.on(''msg'',(e,data) => {
//data=消息对象
})场景 2:渲染进程给主进程发送异步消息,主进程接收到异步消息以后通知渲染进程
//渲染进程
const { ipcRenderer } = require('electron')
ipcRenderer.send('msg',{name:'张三'}); //异步//主进程
const { ipcMain } = require('electron');
ipcMain.on('msg',(event,arg)=> {
event.sender.send('reply','pong');
})//渲染进程
const { ipcRenderer } =require('electron');
ipcRenderer.on('reply',function(event, arg) {
console.log(arg); // prints "pong"
});场景 3:渲染进程给主进程发送同步消息
//渲染进程
const { ipcRenderer } = require('electron')
const msg = ipcRenderer.sendSync('msg-a');
console.log(msg)//主进程
ipcMain.on('msg-a',(event)=> {
event.returnValue = 'hello';
})场景 4:主进程通知渲染进程执行操作
//主进程
BrowserWindow.getFocusedWindow().webContents.send('replay','new 111');//渲染进程
const { ipcRenderer } =require('electron');
ipcRenderer.on('reply',function(event, arg) {
console.log(arg); // prints "pong"
});
川公网安备 51010702003150号
留下您的脚步
最近评论