const {BrowserWindow, ipcMain} = require("electron")
// list of active windows
const windows = []
// method to create windows in your app
const createWindow = (fileToLoad) => {
const win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
})
// add new window to list of active windows
windows.push(win)
// automatically remove window from list when closed
win.on("closed", () => {
const winId = windows.indexOf(win)
winId > -1 && windows.splice(winId,1)
})
fileToLoad && win.loadFile(fileToLoad)
return win
}
// broadcast to all windows opened with createWindow
const broadcast = (eventType, ...params) => {
windows.forEach(win => win.webContents.send("app:broadcastEvent", {eventType, eventData: [...params]}))
}
// later in your code
broadcast('customEvent', "parameter1", "parameter2")
3条答案
按热度按时间yizd12fk1#
您正在寻找
webContents
API,请参阅您帖子中的同一页文档:也可以将消息从主进程发送到呈现器进程,有关详细信息,请参阅webContents.send。
这是webContents的文档
f1tvaqid2#
你可以创建一个窗口数组,然后遍历它们并向每个窗口发送一条消息:
q5iwbnjs3#
详细阐述“约书亚的回答”
在你的main.js中你可以添加这样的内容
在preload.js中,您应该公开一个方法来注册广播事件的侦听器
那么在renderer.js中,你可以像这样注册一个监听器: