问题
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