技术思绪摘录旅行笔记
Electron一个使用HTML、CSS和JavaScript开发桌面应用程序的框架。Electron可以生成在Windows、macOS和Linux上运行的应用程序,借助Electron可以把我们的web端应用直接移植到桌面端而无需再次开发,这样我们可以使用同一套代码在不同平台上运行应用,极大的缩短了开发时间。

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"
});


CarsonIT 微信扫码关注公众号 策略、创意、技术

留下您的脚步

 

最近评论

查看更多>>

站点统计

总文章数:275 总分类数:18 总评论数:88 总浏览数:124.95万

精选推荐

阅读排行

友情打赏

请打开您的微信,扫一扫