连接webSocket客户端时,节点httpServer无法关闭

时间:2014-03-11 06:36:53

标签: javascript node.js websocket

测试节点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。**

更新

我修改了代码,以便在httpServer之前关闭webSocketServer,但仍然存在httpServer。

1 个答案:

答案 0 :(得分:0)

我更新了我的gitHub

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

查找 How do I shutdown a Node.js http(s) server immediately?

Force close all connections in a node.js http server

需要销毁

connection->套接字。

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

http://wiki.ecmascript.org/doku.php?id=harmony:observe#object.observe http://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);