【问题标题】:Can't close the window with Electron and Socket io无法使用 Electron 和 Socket io 关闭窗口
【发布时间】:2017-06-13 03:25:43
【问题描述】:

我用 NodeJS 和 Socket io 开始了一个聊天应用程序项目,一切都很好。

后来我决定将我的应用添加到 Electron 框架中,聊天在一个窗口中开始,但我无法关闭这个窗口,退出按钮什么也不做。

在对我的代码进行一些研究以了解问题出在哪里后,我删除了 main.html 中的 socket.io.js 行,然后我可以关闭我的应用程序,但可以肯定我的所有客户端 Websocket 都停止工作了。

<script src="/socket.io/socket.io.js"></script>

这是我的 main.js 中的 createWindow 函数。

function createWindow () {

  // Instantiate Express App
  app.server = require(__dirname + '/app/app')();

  // Create the browser window.
  win = new BrowserWindow();
  // win.maximize();

  // and load the index.html of the app.
  win.loadURL('http://localhost:'+config.server.port);
  // Open the DevTools.
  // win.webContents.openDevTools();

  win.focus();

  // Emitted when the window is closed.
  win.on('closed', () => {
    console.log("close");
    // 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.
    win = null
  });
}

我的项目文件树是这样的

main.js // Electron, create the window load the app.js
/app/app.js // Express, all my socket function
/views/main.html // Html

请帮帮我!

【问题讨论】:

    标签: javascript node.js express socket.io electron


    【解决方案1】:

    erff 在我的 main.html 中解决了,我有这个功能,删除所有内容后就可以了!

      /** 
      * Alert when user leave the page
      * 
      */
      window.onbeforeunload = function (event) {
        var message = 'Sure you want to leave?';
        if (typeof event == 'undefined') {
          event = window.event;
        }
        if (event) {
          event.returnValue = message;
        }
        return message;
      }
    

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 2015-09-19
      • 2015-11-18
      • 2019-09-30
      • 2015-01-27
      • 2013-09-05
      • 2021-01-30
      • 2017-07-06
      • 1970-01-01
      相关资源
      最近更新 更多