当前位置: 首页 > 知识库问答 >
问题:

渲染器进程未接收到从主进程启动的电子通道消息

章绪
2023-03-14

我正在构建一个带有操作系统托盘菜单的电子应用程序。

在主进程中,我希望听到对“About”菜单项的单击,然后通知渲染器进程,以便它可以相应地更新窗口的视图。

以下是渲染窗口和任务栏菜单时的主要流程部分:

const {app, BrowserWindow, Menu, Tray} = require('electron')
const appConfig = require('./appConfig')
const ipc = require('electron').ipcMain
const path = require('path')
const config = require('../config')

let win, tray

function createWindow(){
    win = new BrowserWindow({
        height: appConfig.window.height,
        width: appConfig.window.width
    })

    win.loadURL(`file://${__dirname}/client/index.html`)

    if(appConfig.debugMode){
        win.webContents.openDevTools()
    }

    win.on('closed', () => {
        win = null
    })
}

function setTrayIcon(){
    tray = new Tray(path.resolve(__dirname, './assets/rocketTemplate.png'))
    tray.setToolTip('Launch applications by group')

    let menuitems = config.groups.map(group => {
        return {label: `Launch group: ${group.name}`}
    })
    win.webContents.send('ShowAboutPage' , {msg:'hello from main process'});
    win.webContents.send('ShowAboutPage')

    menuitems.unshift({type: 'separator'})

    // Here where I add the "About" menu item that has the click event listener
    menuitems.unshift({
        label: 'About AppLauncher',
        click(menuitem, browserWin, event){
            // sending a log to the console to confirm that the click listener did indeed hear the click
            console.log('about menu clicked')
            win.webContents.send('ShowAboutPage')
        }
    })
    menuitems.push({type: 'separator'})
    menuitems.push({label: 'Quit AppLauncher'})

    const contextMenu = Menu.buildFromTemplate(menuitems)
    tray.setContextMenu(contextMenu)
}

app.on('ready', () => {
    createWindow()
    setTrayIcon()
})

下面是应该侦听通道消息的渲染器脚本:

const Vue = require('vue')
const App = require('./App.vue')
const ipc = require('electron').ipcRenderer

ipc.on('ShowAboutPage', () => {
    console.log('show about fired in store')
    alert('show about fired in store')
    this.notificationMessage = 'Show about page'
})


require('./style/base.sass')

new Vue({
    el: '#app',
    render: h => h(App)
})

当我点击托盘中的“关于”菜单时,我从主进程中获得console.log输出,但我从未在渲染器进程中收到console.log或警报。

我的应用程序只有一个窗口(窗口关闭时win变量无效证明了这一点),因此我不会将消息发送到错误的渲染器进程。

我查看了使用webContents向渲染器实例发送消息的电子留档,看起来我的代码结构正确,但显然我错过了一些东西。

有什么想法吗?

共有1个答案

狄钧
2023-03-14

尝试将渲染器脚本重写为

const { ipcRenderer } = require("electron");
ipcRenderer.on("ShowAboutPage", () => {
    //etc
});
 类似资料:
  • 从渲染进程到主进程的异步通信 进程: 渲染进程​ ipcRenderer模块是EventEmitter类的一个实例。 它提供了几个方法,所以你可以从渲染进程(网页)发送同步和异步消息到主进程。您还可以从主流程接收回复。 事件方法 ipcRenderer.on(channel, listener) 用途:监听 channel,并调用 listener(event, args...) 处理新消息 ch

  • 在主进程中处理由渲染进程发起的异步通信. 进程: 主进程​ ipcMain 模块是类EventEmitter类的一个实例. 浅显的打个比方,渲染进程给主进程挂个号,这里就开始忙活起来.当然,你也可以从主进程中向渲染进程发送消息. 发送消息 如果从主进程向渲染进程发送消息,请查看 web-contents-send​ 发送消息,事件名为 channel. 回应同步消息, 请设置 event.retu

  • remote模块是一种渲染器进程(网页)和主进程之间通信(IPC)的简单方法。 进程: 渲染进程​ 在Electron中,GUI相关模块(例如 dialog, menu等)只能用在主进程而非渲染器进程中使用。 为了从渲染器进程使用它们, ipc模块是向主进程发送进程间消息所必需的。 remote模块可以调用主进程对象的方法,而类似于Java的RMI无需显式地发送进程间消息。 从渲染器进程创建浏览器

  • 由于 Electron 使用 Chromium 显示网页,那么,Chromium 的多进程架构也被使用。Electron 中的每个网页都在自己的进程中运行,称为渲染器进程 (renderer process)。 在正常的浏览器中,网页通常运行在沙盒封装化的环境中,并且不允许访问本机资源。然而,Electron 用户有权在网页中使用 Node.js 的 API,从而允许较低级别的操作系统交互。 选自

  • 问题内容: 我正在尝试从某个门户网站获取交易状态,并且在我的Java应用程序中使用了以下chrome设置, 超时从渲染器接收消息:60.000 并且所有待处理的交易都已超时。 会话信息:headless chrome = 68.0.3440.75 驱动程序信息: chromedriver = 2.38 (0) 平台= Linux 2.6.32-696.23.1.el6.x86_64 x86_64)

  • 问题内容: 我想产生长时间运行的子进程,这些子进程在主进程重新启动/死亡时仍然存在。从终端运行时,这工作正常: 请注意,父进程被杀死后,子进程仍处于活动状态。但是,如果我像这样从systemd启动主进程… …然后当我杀死主要过程时,孩子也死了: 我怎样才能使孩子生存呢? 在CentOS Linux版本7.1.1503(Core)下运行go版本go1.4.2 linux / amd64。 问题答案: