Javascript 何时使用远程 vs ipcRenderer,ipcMain

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36548228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:13:08  来源:igfitidea点击:

When to use remote vs ipcRenderer, ipcMain

javascriptelectron

提问by user1513388

I'm trying to understand the communications between electrons main and renderer processes. The documentation https://github.com/electron/electron/blob/master/docs/api/remote.mdstates that the "remote module provides a simple way to do inter-process communication (IPC) between the renderer process and the main process."

我试图了解电子主进程和渲染器进程之间的通信。文档https://github.com/electron/electron/blob/master/docs/api/remote.md指出“远程模块提供了一种在渲染器进程和主要流程。”

However, the documentation is very sparse with regards to how to set it up.

但是,关于如何设置它的文档非常稀少。

I can get the IPC examples to work with my application which seems simple enough. In what scenarios should the remote module be used ?

我可以让 IPC 示例与我的应用程序一起使用,这看起来很简单。远程模块应该在什么场景下使用?

回答by ccnokes

From the remotedocs:

远程文档:

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');

在 Electron 中,GUI 相关的模块(如对话框、菜单等)仅在主进程中可用,在渲染器进程中不可用。为了从渲染进程使用它们,ipc模块需要向主进程发送进程间消息。使用远程模块,您可以调用主进程对象的方法,而无需显式发送进程间消息,类似于Java 的 RMI。从渲染器进程创建浏览器窗口的示例:

const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;

var win = new BrowserWindow({ width: 800, height: 600 });
win.loadURL('https://github.com');

Basically the remotemodule makes it easy to do stuff normally restricted to the main process in a render process without lots of manual ipc messages back and forth.

基本上,该remote模块可以轻松地在渲染过程中执行通常仅限于主过程的事情,而无需来回手动输入大量 ipc 消息。

So, in a renderer process, instead of:

因此,在渲染器进程中,而不是:

const ipc = require('electron').ipcRenderer;
ipc.send('show-dialog', { msg: 'my message' });
ipc.on('dialog-shown', () => { /*do stuff*/ });

(And then code in the main to do stuff in response to those messages).

(然后在 main 中编写代码来响应这些消息)。

You can just do this all in the renderer:

你可以在渲染器中完成这一切:

const remote = require('electron').remote;
const dialog = remote.require('dialog')

dialog.showErrorBox('My message', 'hi.');

The ipc module is not explicitly required (although it's happening for you behind the scenes). Not to say the two are mutually exclusive.

ipc 模块不是明确需要的(尽管它在幕后为您发生)。并不是说两者是相互排斥的。

One further question when using remote. Is it possible to access a function that exists in the main process rather than a module ?

使用遥控器时的另一个问题。是否可以访问存在于主进程而不是模块中的函数?

I think what you're really asking is: how can I share code between main/renderer processes and how do I require a module in the renderer?

我认为您真正要问的是:如何在主进程/渲染器进程之间共享代码以及如何在渲染器中要求模块?

EDIT: You can just require it like normal. An edge case of this is if your renderer window's current URL isn't pointed to a local file (not loaded using file://). If you're loading a remote URL, your require path needs to be absolute or you can use remote like I said below.

编辑:您可以像平常一样要求它。这种情况的一个边缘情况是,如果您的渲染器窗口的当前 URL 未指向本地文件(未使用 file:// 加载)。如果您正在加载远程 URL,则您的 require 路径需要是绝对路径,或者您可以像我下面所说的那样使用 remote。

OLD:

老的:

This is another use case for remote. For example:

这是remote. 例如:

remote.require('./services/PowerMonitor.js')

Note that using remote like that causes your code to be run in the context of the main process. That might have it's uses but I would be careful.

请注意,像这样使用远程会导致您的代码在主进程的上下文中运行。这可能有它的用途,但我会小心。

Built-in node modules or electronbe required like normal:

内置节点模块或electron正常需要:

require('electron')
require('fs')

Can I access global variables from the renderer?

我可以从渲染器访问全局变量吗?

Yes.

是的。

//in main
global.test = 123;
//in renderer
remote.getGlobal('test') //=> 123