2017-01-27 50 views
2

我开始了一个使用NodeJS和Socket io的聊天应用程序项目,一切都很好。无法用电子和插座关闭窗口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 

请帮帮我!

回答

2

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; 
    }