Broadcasting message from ipcMain in electron

蹲街弑〆低调 提交于 2019-12-25 08:39:40

问题


I need to broadcast a message from main process of electron to all renderer processes. There is no send option for ipcMain, only an option to reply to the sender via event.sender.send().


回答1:


You are looking for the webContents API. From the same page of documentation in your post:

It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.

Here is the doc for webContents




回答2:


You could make an array of windows, then iterate over them and send a message to each one:

var windowsArr = [];

windowsArr.push(new BrowserWindow({title: "Win 1"}));
windowsArr.push(new BrowserWindow({title: "Win 2"}));

function broadcast (message) {
    for (var i = 0; i < windowsArr.length; i++) {
        windowsArr[i].webContents.send('asynchronous-message', message);
    }
}


来源:https://stackoverflow.com/questions/44126768/broadcasting-message-from-ipcmain-in-electron

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