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