How to show data received in a communication between BrowserWindows using Electron

0

I have 2 BrowserWindow:

let AlgoWin = new BrowserWindow({width: 1280, height: 730);
let AlgoMasWin = new BrowserWindow({width: 1120, height: 550);

These 2 BrowserWindow load html files hosted in the public_html folder such that:

AlgoWin.loadFile("public_html/index.html");

AlgoMasWin.loadFile("public_html/algoMas.html");

As I understand, to pass data between BrowserWindows it could be done like this:

AlgoWin.webContents.on('did-finish-load', function() {
   AlgoWin.webContents.send('configInfo', Data);
});

My question comes at this point. Taking into account that the index.html is executed as if it were on the client's side (therefore I can not use require), as it could do to receive the data that it wants to send from the main process of the application to index.html and use those data to show?

    
asked by alex55132 27.08.2018 в 00:23
source

1 answer

2

You have to add an ipcRenderer listener inside the HTML, in case of having node disabled you must preload it in the BrowserWindow constructor in the following way:

  • You must create a script that contains a node reference to ipcRenderer and anchor it to the window object.
  • preinit.js

    window.ipcRenderer = require('electron').ipcRenderer;
    
  • Define the preload in the constructor with nodeIntegration disabled (by default).
  • main

    let AlgoWin = new BrowserWindow({
         width: 1280, 
         height: 730,
         webPreferences: {
            nodeIntegration: false,
            preload: __dirname + '/preinit.js'
          }
    });
    
  • From here you can add the Event Listener inside the html using the ipcRenderer reference using JavaScript.
  • index.html

    <html>
    <body>
      <script>        
        window.ipcRenderer.on('configInfo', function (event, midata) {
           console.log(midata); // procesar aca
        });
      </script>
    </body>
    </html>
    
        
    answered by 04.09.2018 / 16:11
    source