How To Send Messages To Different Windows In Electron?
I'm writing my first electron app, so please be lenient :) When the user presses a button on the main Window, there should open a new window which shows some json string. This even
Solution 1:
The problem is this code:
ipcMain.on('JSON_PAGE:Ready', function(event, arg) {
jsonWin.webContents.send('JSON:Display', item);
})
Every time you create a new window, you are having ipcMain
subscribe to the same message. This means that when ipcMain
gets the 'JSON_PAGE:Ready'
message, it calls every single callback it has registered and sends a message to every single window.
The simplest solution in this case is to use the event that's passed to the ipcMain
handler to send the message to the renderer that sent it to main. Second, subscribe a single time outside of createJSONWindow
:
ipcMain.on('JSON_PAGE:Ready', function(event, arg) {
e.sender.send('JSON:Display', item);
});
function createJSONWindow() { ... }
However, is 'JSON:Display'
simply sent when the page has loaded? If so, you can subscribe the window's webContents to the did-finish-load
event which fires when the page has loaded.
jsonWin.webContents.on("did-finish-load", () => {
jsonWin.webContents.send(...);
});
Post a Comment for "How To Send Messages To Different Windows In Electron?"