Electron - Can my app communicate with the main and renderer processes?

吃可爱长大的小学妹 提交于 2019-12-05 11:14:47

I suppose you are talking about the main process and other browser windows.

You can use BrowserWindow.webContents.send(channel[, arg1][, arg2][, ...]) to send messages from the main process to to a browser window, and receive it using ipcRenderer. Take this example:

Main process:

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

The BrowserWindow called subWindow:

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

When you want to send data from the browser window to the main process, use remote.app.emit. Receive it using app.on. The same example:

Main process:

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

subWindow:

require("electron").remote.app.emit("test","hey!");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!