I'm in big trouble. I checked other solutions but nothing helped me. I don't understand why the "send" method doesn't work here. No exceptions, no messages or anything else.
I tried wrapping the send method in another method but it failed. I tried using the "sendToFrame" method but it failed too.
main:
const { app, BrowserWindow, ipcMain } = require('electron')
let win;
function createWindow () {
win = new BrowserWindow({
width: 800,
height: 600,
frame: true,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: true,
contextIsolation: false, // false if you want to run 2e2 test with Spectron
enableRemoteModule: true,
}
});
win.loadFile('src/home.html');
win.webContents.openDevTools();
ipcMain.on('main', (event, msg) => {
sendData() // Does not work.
event.reply('new-client', 'res') // It works.
})
function sendData() {
win.webContents.send('new-client', {wsh: 'wshsdqsdqs'});
}
};
app.whenReady().then(createWindow);
Below, the "ipcRenderer.send" method works. It sends data to the main process but receives no response when I use the "webContents.send" method (in the main process).
new-client.js:
const { ipcRenderer } = require('electron');
const {resolve} = require('path');
const {Message} = require(resolve('src/js/shared/class/message/message.js'));
ipcRenderer.on('new-client', (event, arg) => {
console.log(event)
console.log(arg)
})
async function send() {
const msg = new Message(
'new-client',
'clients',
true
);
ipcRenderer.send('main', msg);
}
EDIT:
I forgot this... I sent the data to the wrong window.
You can use BrowserWindow.getAllWindows, then send the data to a specific window.
Personally, I added an event when windows are open, in this way I can properly send data and manage windows.