简体   繁体   English

Electron工控机通讯

[英]Electron ipc communication

i was working on a electron app, and tried to use ipc communication to call a function in my index.js file, that holds the authentication system.我正在开发一个 electron 应用程序,并尝试使用 ipc 通信在我的 index.js 文件中调用一个 function,该文件包含身份验证系统。 I need to have this function inside of the ipcMain.on("login", (event, data) => {}) event.我需要在ipcMain.on("login", (event, data) => {})事件内有这个 function 。 But sadly the function launch didnt work, and i haven't got any errors in console.但遗憾的是,function launch不起作用,我在控制台中没有任何错误。 So there is a diffrent way of doing this i assume.所以我假设有一种不同的方式来做到这一点。 Any help would be gladly appreciated任何帮助将不胜感激

My index.js file:我的 index.js 文件:

const { app, BrowserWindow, ipcMain } = require('electron')


function createWindow() {
  // Create the browser window.
  var mainWindow = new BrowserWindow({
    title: "AstroLauncher",
    width: 1000,
    height: 660,
    
    webPreferences: {
      nodeIntegration: true
    }
    
  })
  mainWindow.setMenuBarVisibility(false)
  mainWindow.loadFile("home.html");
  return mainWindow;

}



app.whenReady().then(() => {
   const mainWindow = createWindow()


    app.on('activate', function () {
        if(BrowserWindow.getAllWindows.length === 0) createWindow()
    })

app.on('window-all-closed', function () {
    if(process.platform !== 'darwin') app.quit()
})

ipcMain.on("login", (event, data) => {
    

    Authenticator.getAuth(data.u, data.p).then(() => {
    event.sender.send('done')
    mainWindow.loadFile("home.html");   

    ipcMain.on('launch', () => {
      function launch() {
        alert('launched!');
      }
    })


 
}).catch((err) => {
   event.sender.send("err", { er: err })
    })
})

}).catch(err => console.log(err))

and my home.html file:和我家的.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">.
    <script src="./index.js"></script>
 </head>
<body>
  <input type="button" value="Launch!" id="lanBTN" >

<script>
            const ipc = require('electron').ipcRenderer


    document.querySelector('#lanBTN').addEventListener("click", () => {
      ipc.send('launch')
    })
   
    </script>
</body>
</html>

  ipcMain.on('launch', () => {
     function launch() {
        alert('launched!');
      }
     launch() ;
 })

I think you forgot to call launch() or you can this way我想你忘了打电话 launch() 或者你可以这样

  ipcMain.on('launch', () => {
        alert('launched!');
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM