【问题标题】:Electron - Can my app communicate with the main and renderer processes?Electron - 我的应用程序可以与主进程和渲染器进程通信吗?
【发布时间】:2017-10-07 03:35:17
【问题描述】:

我写了一个非常非常基本的电子应用程序——标准的 hello world 类型,你基本上有一个 HTML 文件,上面写着“Hello, World”——它位于电子的“app”目录中,然后是运行应用程序时通过 main.js 加载。

现在,假设我希望能够从我的应用程序中的 javascript 与其中任何一个进程(主进程或渲染器,最好是两者!)进行通信,可以做到吗?我真的无法在网上找到任何关于它的信息 - 但我的主要问题可能是我什至不知道首先要搜索什么。我对 Electron 很陌生。

【问题讨论】:

  • 您要搜索“ipc”(进程间通信)和“electron”。有很多方法可以做到这一点。这家伙有一堆很棒的视频/博客教程,关于 Electron 中的 ipc、调试等:electron.rocks/video-series

标签: electron


【解决方案1】:

我想你说的是主进程和其他浏览器窗口。

您可以使用BrowserWindow.webContents.send(channel[, arg1][, arg2][, ...]) 将消息从主进程发送到浏览器窗口,并使用ipcRenderer 接收它。举个例子:

主要流程:

subWindow.webContents.send("foo","bar");

BrowserWindow 称为subWindow

var ipc=require("electron").ipcRenderer;
ipc.on("foo",(event, arg1) => {
    console.log(arg1); //Outputs "bar"
});

当你想从浏览器窗口向主进程发送数据时,使用remote.app.emit。使用app.on 接收它。同样的例子:

主要流程:

var app=require("electron").app;
app.on("test",(arg) => {
    if (arg=="hey!") console.log("ha!");
}

子窗口:

require("electron").remote.app.emit("test","hey!");

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 2017-06-01
    • 2021-12-04
    • 2020-04-09
    • 2021-07-24
    • 2019-02-19
    • 2019-05-14
    • 1970-01-01
    • 2017-05-01
    相关资源
    最近更新 更多