主进程和呈现器进程之间的电子设置IPC通信 [英] Electron - Setting up IPC communication between main and renderer processes

查看:23
本文介绍了主进程和呈现器进程之间的电子设置IPC通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用https://github.com/maxogden/menubar创建一个带有Electron的菜单栏桌面应用程序。然而,我正在努力建立基本的IPC通信。你知道为什么下面的代码不能工作吗?为了澄清这一点,我希望在应用程序启动时test注销到控制台,但事实并非如此。

app.js

const { app } = require('electron');
const Menubar = require('menubar');

const menubar = Menubar.menubar({
  index: `file://${__dirname}/index.html`,
  preloadWindow: true,
  icon: './assets/img/icon.png',
});

try {
  require('electron-reloader')(module)
} catch (_) { }

app.on('ready', () => {
  menubar.window.webContents.send('test');
});

renderer.js

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

ipcRenderer.on('test', () => {
  console.log('test');
});

index.html

<html>
<head>
  <title>Test</title>
  <script>
    require('./renderer')
  </script>
</head>
<body>
</body>
</html>

推荐答案

这可能是假阴性。

我希望在应用程序启动时将测试注销到控制台,但事实并非如此。

console.log调用的输出位置取决于进行这些调用的位置:

  • 从主线程:查看启动应用程序的终端。

  • 从呈现器线程:查看Chrome DevTools控制台。

因此,请确保您在正确的位置查找预期输出。

如果这不起作用,那么这里有一个关于如何在主进程和呈现器进程之间设置IPC通信的小演示。

main.js

您会注意到,我确实将nodeIntegrationcontextIsolation都设置为其缺省值。这样做是为了明确您不需要降低应用程序的安全门槛以允许主进程和呈现器进程之间的消息。

这里发生了什么?

主进程等待呈现器完成加载,然后才发送其ping";消息。IPC通信将由预加载脚本处理。

注意console.log调用,并查看它在下面的截屏视频中出现的位置。

const {app, BrowserWindow} = require('electron'); // <-- v15
const path = require('path');

app.whenReady().then(() => {
  const win = new BrowserWindow({
    webPreferences: {
      devTools: true,
      preload: path.resolve(__dirname, 'preload.js'),
      nodeIntegration: false, // <-- This is the default value
      contextIsolation: true  // <-- This is the default value
    }
  });
  win.loadFile('index.html');
  win.webContents.openDevTools();
  win.webContents.on('did-finish-load', () => {
    win.webContents.send('ping', '🏓');
  });
  // This will not show up in the Chrome DevTools Console
  // This will show up in the terminal that launched the app
  console.log('this is from the main thread');
});

preload.js

我们正在使用contextBridge接口。这允许向呈现器进程公开特权API,而无需启用nodeIntegration或中断上下文隔离。

该API将在一个非常愚蠢的命名空间(玉米煎饼)下提供,以表明您可以更改这一点。

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

contextBridge.exposeInMainWorld('BURRITO', {
  whenPing: () => new Promise((res) => {
    ipcRenderer.on('ping', (ev, data) => {
      res(data);
    });
  })
});

renderer.js

使用预加载脚本提供的API,我们开始侦听ping消息。当我们得到它时,我们将主进程传递的数据放在呈现器页面中。我们还记录了一条消息,您可以在下面的截屏视频中看到。

BURRITO.whenPing().then(data => {
  document.querySelector('div').textContent = data;
  // This will show up in the Chrome DevTools Console
  console.log(`this is the renderer thread, received ${data} from main thread`);
});

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>IPC Demo</title>
  </head>
  <body>
    <div></div>
    <script src="./renderer.js"></script>
  </body>
</html>

使用以下命令运行应用:

npx electron main.js

您可以看到,这两个console.log调用在两个不同的位置生成了输出。

这篇关于主进程和呈现器进程之间的电子设置IPC通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆