electron 主进程和渲染进程通信

1、主进程引入
	const {ipcMain} = require('electron')

2、渲染进程引入
	const {ipcRenderer}=require('electron');
	
3、通信
	(1)两个进程互相通信
		主进程:
		 接收:ipcMain.on('主进程名‘,(event,ans)=>{
		    ans为渲染进程发送的数据
		   	
		   	发送:
		    event.reply('渲染进程名',数据);
		  })
		  
		  
		渲染进程:
		  
		  接收:ipcRenderer.on('渲染进程名',(event,ans)=>{
			ans为主进程发送的数据
		  })
		  发送:ipcRenderer.send("主线程名",数据);
			
		
	(2)主进程->渲染进程
		主线程发送:mainWindow.webContents.send('渲染进程名',数据)
			 直接发送速度太快,子进程还未创建,接收不到,通过定时器延迟触发
		渲染进程接收:ipcRenderer.on('渲染进程名',(event,ans)=>{...})

ipcMain
ipcRenderer

代码示例:
主进程:

// Modules to control application life and create native browser window
const {app, BrowserWindow,BrowserView,globalShortcut,ipcMain} = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    //弹出的窗口有无边框,默认为有
    // frame:false,
    show:false,
    backgroundColor:'#586148',
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration:true,
      webviewTag:true
    },
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')
  mainWindow.webContents.on("did-finish-load",()=>{
      
  })
  mainWindow.webContents.on('dom-ready',()=>{
   
  })
  mainWindow.once('ready-to-show',function(){
    mainWindow.show();
  })

  setTimeout(()=>{
    mainWindow.webContents.send('fromMain','哇啦啦')
  },2000)
  
  //接收渲染进程
  ipcMain.on('send',(event,ans)=>{

    console.log('主进程接收'+ans);
    //发送渲染进程
    event.reply('sendToRender','来自主进程');
  })




  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
// app.whenReady().then(createWindow)
app.on('ready',()=>{

  createWindow();


});

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q

  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

渲染进程:


const {ipcRenderer}=require('electron');

var view=document.querySelector('.wb');
var sp=document.querySelector('.sp');
var btn=document.querySelector('.obtn');
var gbtn=document.querySelector('.gbtn');
var cbtn=document.querySelector('.cbtn');
var dbtn=document.querySelector('.dbtn');

var proxy;

btn.onclick=function(){
    //发送给主进程
    ipcRenderer.send("send",'来自渲染进程');
}
//接收主进程
ipcRenderer.on('sendToRender',(event,ans)=>{
    console.log(ans);
})

ipcRenderer.on('fromMain',(event,ans)=>{
    console.log(ans);
})

gbtn.onclick=function(){

}

cbtn.onclick=function()
{

}

dbtn.onclick=function()
{

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值