当webSocket客户端连接时,节点httpServer关闭失败



测试节点httpServer和webSocketServer(ws)

尝试实现一个http-ws服务器,该服务器将在客户端连接后关闭,如下所示:

var HTTPserver =
    httpServer(require('path')
        .join(__dirname, 'www'));
HTTPserver
    .on('listening', function()
    {
        console.log('HTTP listening:');
        //---------------
        var WebSocket = require('ws');
        var webSocketServer =
            new WebSocket.Server(
            {
                server: HTTPserver
            });
        webSocketServer
            .on('connection',
                function(ws)
                {
                    console.log('client connected');
                    setTimeout(function()
                    {
                        console.log('trying to close webSocketserver');
                        webSocketServer.close();
                        setTimeout(function()
                        {
                            console.log('trying to close HTTPserver');
                            HTTPserver.close();
                        }, 1000);
                    }, 1000);
                });
    })
    .on('close', function()
    {
        console.log('server closed'); // never happens
    })
    .on('error', function()
    {
        console.log('server error');
    })
    .listen(8000);
// this works when no Client Connection
/*
setTimeout(function()
{
    HTTPserver.close();
}, 3000);
*/

我上传了完整的测试项目。https://github.com/kenokabe/http-ws-test

HTTPserver.close();在没有ws客户端连接的情况下正常工作

HTTPserver.close();ws客户端连接不工作(关闭失败)

知道发生了什么以及如何解决这个问题吗?谢谢。

* * PS。请注意,我需要关闭的是

不仅ws连接,而且httpServer。

我需要经常打开和关闭整个。

关闭httpServer的原因是这个项目是用于插件的,在特殊情况下需要关闭httpServer .**

我修改了代码关闭webSocketServer之前httpServer,但仍然httpServer是活的。

我更新了我的gitHub

https://github.com/kenokabe/http-ws-test

发现我如何关闭一个Node.js http(s)服务器立即?

强制关闭node.js http服务器中的所有连接

connection->socket需要销毁。

对于我的示例代码,我使用ES6 observe方法。

http://wiki.ecmascript.org/doku.php?id=harmony观察# object.observehttp://wiki.ecmascript.org/doku.php?id=harmony: observe_api_usage

https://github.com/jdarling/Object.observe

var IO = {
    terminate:
    {
        val: false
    }
};
var trigger = function(obj)
{
    obj.val = !obj.val;
};
var HTTPserver =
    httpServer(require('path')
        .join(__dirname, 'www'));
var webSocketServer; 
HTTPserver
    .on('listening', function()
    {
        console.log('HTTP listening:');
        //---------------
        var WebSocket = require('ws');
        webSocketServer =
            new WebSocket.Server(
            {
                server: HTTPserver
            })
            .on('connection',
                function(ws)
                {
                    console.log('client connected');
                });
    })
    .on('close', function()
    {
        console.log('server closed'); // never happens
    })
    .on('error', function()
    {
        console.log('server error');
    })
    .on('connection', function(socket)
    {
        console.log('connection');
        Object.observe(IO.terminate, function(changes)
        {
            console.log('now!2');
            socket.destroy();
        });
    })
    .listen(8000);
setTimeout(function()
{
    console.log('now');
    HTTPserver.close();
    webSocketServer.close();
    trigger(IO.terminate);
}, 15000);

相关内容

  • 没有找到相关文章

最新更新