Broadcasting message from ipcMain in electron

2019-08-16 17:33发布

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().

标签: electron
2条回答
爷的心禁止访问
2楼-- · 2019-08-16 17:35

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

查看更多
祖国的老花朵
3楼-- · 2019-08-16 17:37

You could make an array of windows, then iterate over them and send a message like this:

var windowsArr = [];

windowsArr[1] = new BrowserWindow({title: "Win 1"});
windowsArr[2] = new BrowserWindow({title: "Win 2"});

function broadcast (message) {
    for (var i = 1; i <= windowArr.length; i++) {
        windowArr[i].webContents.send('asynchronous-message', message);
    }
}
查看更多
登录 后发表回答