Broadcasting message from ipcMain in electron

2019-08-16 16:57发布

问题:

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


标签: electron