2017-02-18 2 views
1

Mon processus principal (code important):IPC électronique: Pourquoi mon message ipc n'est-il pas reçu?

let introWindow 
let win = null 

function createWindow() { 
    // Create the browser window. 
    introWindow = new BrowserWindow({ 
      width: 600, 
      height: 480, 
      title: "Glaze", 
      resizable: false 
     }) 
     // and load the index.html of the app. 


    introWindow.loadURL(url.format({ 
      pathname: path.join(__dirname, 'src/html/start.html'), 
      protocol: 'file:', 
      slashes: true 
     })) 

    // Emitted when the window is closed. 
    introWindow.on('closed', function() { 
     // Dereference the window object, usually you would store windows 
     // in an array if your app supports multi windows, this is the time 
     // when you should delete the corresponding element. 
     introWindow = null 
    }) 
} 

function createMainWindow(arg) { 
    const htmlPath = path.join('file://', __dirname, 'src/html/index.html?room=' + arg.room + "&nick=" + arg.nick) 
    win = new BrowserWindow({ 
      width: 800, 
      height: 700, 
      title: "Glaze" 
     }) 
    win.on('closed', function() { 
     win = null; 
    }) 

    win.loadURL(htmlPath) 
    win.show() 

    win.webContents.send('message', "IT WORKED!"); 

    autoUpdater.on('checking-for-update',() => { 
    sendStatus('Checking for update...'); 
    }) 
    autoUpdater.on('update-available', (ev, info) => { 
    sendStatus('Update available.'); 
    }) 
    autoUpdater.on('update-not-available', (ev, info) => { 
    sendStatus('Update not available.'); 
    }) 
    autoUpdater.on('error', (ev, err) => { 
    sendStatus('Error in auto-updater.'); 
    }) 
    autoUpdater.on('download-progress', (ev, progressObj) => { 
    sendStatus('Download progress...'); 
    log.info('progressObj', progressObj); 
    }) 
    autoUpdater.on('update-downloaded', (ev, info) => { 
    sendStatus('Update downloaded. Will quit and install in 5 seconds.'); 
    // Wait 5 seconds, then quit and install 
    setTimeout(function() { 
     autoUpdater.quitAndInstall(); 
    }, 5000) 
    }) 
    // Wait a second for the window to exist before checking for updates. 
    setTimeout(function() { 
    autoUpdater.checkForUpdates() 
}, 5000); 
} 

// This method will be called when Electron has finished 
// initialization and is ready to create browser windows. 
// Some APIs can only be used after this event occurs. 
app.on('ready', function() { 
     const menu = Menu.buildFromTemplate(template) 
     Menu.setApplicationMenu(menu) 
     createWindow() 

}) 

// Quit when all windows are closed. 
app.on('window-all-closed', function() { 
    let reopenMenuItem = findReopenMenuItem() 
    if (reopenMenuItem) reopenMenuItem.enabled = true 
    // On OS X it is common for applications and their menu bar 
    // to stay active until the user quits explicitly with Cmd + Q 
    if (process.platform !== 'darwin') { 
     app.quit() 
    } 
}) 

app.on('activate', function() { 
    // On OS X it's common to re-create a window in the app when the 
    // dock icon is clicked and there are no other windows open. 
    if (introWindow === null && win === null) { 
     createWindow() 
    } 
}) 

// In this file you can include the rest of your app's specific main process 
// code. You can also put them in separate files and require them here. 

app.on('browser-window-created', function() { 
    let reopenMenuItem = findReopenMenuItem() 
    if (reopenMenuItem) reopenMenuItem.enabled = false 
}) 



ipc.on('close-starter', function(event, arg) { 
    createMainWindow(arg); 
}) 





function sendStatus(text) { 
    log.info(text); 
    win.webContents.send('message', text); 
} 

Et mon index.html:

<script> 
    const ipc = require('electron').ipcRenderer; 
    ipc.on('message', function(event, text) { 
     console.log("Message received") 
     console.log(text, event); 
     var container = document.getElementById('messages'); 
     var message = document.createElement('div'); 
     message.innerHTML = text; 
     container.appendChild(message); 
    }) 

    console.log("ran") 
</script> 

Quand je lance mon application, je vérifie la console d'outils de dev dans ma fenêtre win et il n'y a pas messages imprimés sauf ran en notant que le fichier a couru. Pourquoi le message ipc ne passe-t-il pas? win ne doit pas être égal à null à ce moment lorsque le .send est appelé. Le problème peut-il être avec mon fichier principal ou avec mon fichier de réception?

+0

quand je dis la console, je veux dire la console d'outils de dev. Et oui j'ai déjà essayé cela @pergy – adamSiwiec

+0

je vois, OFC. Et si vous envoyez seulement 'on ('dom-ready')'? – pergy

+0

Que voulez-vous dire? – adamSiwiec

Répondre

3

Comme un résumé de mon commentaire, canal ipc peut être utilisé seulement après webContents sont installés avec succès. Par conséquent, vous devez placer votre appel send dans un rappel qui garantit que le canal ipc est en service.

En documentation de send exemple de code fait la même chose:

// In the main process. 
const {app, BrowserWindow} = require('electron') 
let win = null 

app.on('ready',() => { 
    win = new BrowserWindow({width: 800, height: 600}) 
    win.loadURL(`file://${__dirname}/index.html`) 
    win.webContents.on('did-finish-load',() => { 
    win.webContents.send('ping', 'whoooooooh!') 
    }) 
})