- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试了解 Electron 主进程和渲染器进程之间的通信。文档 https://github.com/electron/electron/blob/master/docs/api/remote.md指出“远程模块提供了一种在渲染器进程和主进程之间进行进程间通信(IPC)的简单方法。”
但是,关于如何设置它的文档非常少。
我可以让 IPC 示例与我的应用程序一起工作,这看起来很简单。 remote模块应该在什么场景下使用?
最佳答案
来自remote文档:
In Electron, GUI-related modules (such as dialog, menu etc.) are only available in the main process, not in the renderer process. In order to use them from the renderer process, the ipc module is necessary to send inter-process messages to the main process.With the remote module, you can invoke methods of the main process object without explicitly sending inter-process messages, similar to Java's RMI. An example of creating a browser window from a renderer process:
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
var win = new BrowserWindow({ width: 800, height: 600 });
win.loadURL('https://github.com');
基本上,remote
模块可以很容易地完成通常仅限于渲染进程中主进程的工作,而无需来回发送大量手动 ipc 消息。
因此,在渲染器进程中,而不是:
const ipc = require('electron').ipcRenderer;
ipc.send('show-dialog', { msg: 'my message' });
ipc.on('dialog-shown', () => { /*do stuff*/ });
(然后在主体中编写代码来响应这些消息)。
你可以在渲染器中完成这一切:
const remote = require('electron').remote;
const dialog = remote.require('dialog')
dialog.showErrorBox('My message', 'hi.');
ipc 模块不是明确需要的(尽管它在幕后发生)。并不是说两者是相互排斥的。
One further question when using remote. Is it possible to access a function that exists in the main process rather than a module ?
我认为您真正要问的是:如何在主进程/渲染器进程之间共享代码以及如何在渲染器中需要一个模块?
编辑:您可以像往常一样要求它。如果您的渲染器窗口的当前 URL 未指向本地文件(未使用 file://加载),则这是一种边缘情况。如果您正在加载远程 URL,则您的要求路径必须是绝对路径,或者您可以像我下面所说的那样使用远程。
旧:
这是 remote
的另一个用例。例如:
remote.require('./services/PowerMonitor.js')
请注意,像这样使用远程会导致您的代码在主进程的上下文中运行。这可能有它的用途,但我会小心。
内置节点模块或 electron
像平常一样需要:
require('electron')
require('fs')
Can I access global variables from the renderer?
是的。
//in main
global.test = 123;
//in renderer
remote.getGlobal('test') //=> 123
关于javascript - 何时使用远程与 ipcRenderer、ipcMain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548228/
我们可以为 ipcMain.on 方法和 ipcMain.handle() 注册相同的 channel 吗? 例如: ipcMain.handle('test', async(event,args)
最近,我一直在开发Windows应用程序(使用Electronjs)来加密文件。我想要它,这样当我按下渲染器中的第一个按钮时,它将发送到主进程,要求它打开打开文件对话框。我尝试了这段代码。 rende
它总是返回未定义的,请问有什么想法吗? 我需要在fs.reddir中返回,但相对于ipcMain //main ipcMain.handle('pegaDirRomSalvo', async (eve
我正在 react 中创建一个拖放并使用 Electron 发送和接收我的文件数据。我在 console.log 中收到 senderId 和 sender。我怎样才能让数据出现?我错过了什么? Dr
我有一个函数可以向 ipcMain 发送查询,然后等待运行 SQL 查询。当响应返回时,我遍历返回的数据并将其显示在屏幕上以供用户查看。 出于某种原因,无论何时返回数据并且我从渲染过程中记录它,它都会
下面的代码会产生错误 const ipcMain = require('electron').ipcMain; ipcMain.on('open-file-dialog', function (eve
我正在尝试了解 Electron 主进程和渲染器进程之间的通信。文档 https://github.com/electron/electron/blob/master/docs/api/remote.
这是情况。 可通过main.js中的nodejs(node-adodb)直接访问该数据库。我的Angular应用发送请求并从db.service接收sql结果。这部分工作正常,但是如果我有一个类似ge
我对一件奇怪的事情感到有点困惑。我希望我能得到一些关于这种行为的解释。 这是我的小学习项目。一个简单的tcp客户端-服务器聊天程序。客户端是用Electron编写的,但还没有运行。 我的聊天模块是:
我制作了一个简单的 Electron 应用程序,它使用 ipcMain 和 ipcRenderer。这是代码,这是有道理的: main.js const { app, BrowserWindow, i
我正在用 Electron js 编写一个桌面应用程序,我想将数据从我的主进程发送到我的渲染器进程,反之亦然。为此,我使用 ipcMain和 ipcRenderer ,但很奇怪,同时能够通过我的渲染器
我的应用程序打开了两个窗口,其中一个应该在投影仪上(第二个监视器)打开。我正在尝试使其正常运行,以便如果在未连接投影仪的情况下打开应用程序,则用户可以连接投影仪,然后单击按钮并按下第二个窗口。如果他们
我正在使用 Angular/Electron 构建应用。在 Angular 组件中,我有一个方法 - handleData() - 定义一个对象 - 只是简单的数组、字符串、数字等,没有异步调用或任何
我的问题是当我从渲染器进程调用 ipcMain 操作时渲染器线程被卡住,直到 ipcMain 操作完成(同步而不是异步) 代码示例: 渲染器.js export const startCopy = d
我正在开发一个 Electron 应用程序,总体来说我有一个架构问题。 每当我需要在主进程中完成某些操作时,我都会直接在 main.js 文件中完成它,我相信这是错误的,因为我最终会得到一个非常长的整
我克隆了electronic-quick-start演示,并测试了ipcMain函数。 按照文档在main.js中添加此文件 const { ipcMain } = require('electron
我有一个 Electron 应用程序,它首先启动一个启动器窗口(在渲染器进程中),它启动多个后台服务。在这些后台服务成功启动后,它会发送 "services-running"在其ipcRenderer
Electron 新手我已经知道如何从渲染器发送到 Main,但我正在尝试学习如何从 Main 到渲染器。在我的研究中,我读到: IPC send from main process to rende
在 electron , 可以通过 ipcRenderer.sendSync('synchronous-message', 'ping') 将同步消息从 IpcRenderer 发送到 IpcMain
我正在使用 Electron、Vue + Vuetify 创建桌面应用程序,但是当我尝试使用 IPC 从 rendererProcess 向 mainProcess 发送内容时,会出现白屏和空白屏幕,
我是一名优秀的程序员,十分优秀!