繁体   English   中英

如何在 Electron App Quit 上关闭 React App

[英]How to Close React App on Electron App Quit

I have a Electron + React app that is working on port 3000. When I call the app.quit method from Electron's start.js file, it exits from the Electron but I can still see that 0.0.0.0:3000 is in LISTENING state from网络统计-ao。

我正在研究 Windows 10 并且我尝试了 electron 文件中的 app.exit(0) ,但是仍然可以从浏览器访问 React 应用程序,并且 netstat -ao 在 LISTENING Z9ED39E2EA9341586 中显示 0.0.0.0:3000。

  "scripts": {
    "start": "nf start -p 3000",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "electron": "electron .",
    "electron-start": "node src/start-react",
    "react-start": "SET BROWSER=none&&react-scripts start",
    "pack": "build --dir",
    "dist": "npm run build && build",
    "postinstall": "install-app-deps",
    "preinstall": "npm install -g foreman"
  },

我想在调用 Electron 的 app.quit() 之后/之前完全关闭 ReactApp 这是我用于关闭应用程序的代码。

ipcMain.on('closeApp', (evt, arg) => {
    app.exit(0)
});

您看到0.0.0.0:3000仍在收听,因为您没有正确关闭连接。 您需要在退出应用程序之前关闭反应应用程序服务器。 electron app中有一个名为before-quit的事件恰好在应用程序退出之前触发。 您可以利用回调关闭服务器。 查找连接到端口3000的pid ,有一个find-process package,找到pid后,杀死该进程。 考虑给定的例子。

const find = require('find-process');

app.on('before-quit' , (e) => {
    find('port', 3000)
      .then(function (list) {
      if(list[0] != null){
          process.kill(list[0].pid, 'SIGHUP');
      }
    }.catch((e) => {
        console.log(e.stack || e);
    });
});

暂无
暂无

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

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