使用NodeJS删除JavaScript中的websocket连接

时间:2013-05-20 23:36:25

标签: arrays node.js websocket connection

                            #!/usr/bin/env node
                            var WebSocketServer = require('websocket').server;
                            var http = require('http');

                            var server = http.createServer(function(request, response) {
                                console.log((new Date()) + ' Received request for ' + request.url);
                                response.writeHead(404);
                                response.end();
                            });
                            server.listen(8080, function() {
                                console.log((new Date()) + ' Server is listening on port 8080');
                            });

                            wsServer = new WebSocketServer({
                                httpServer: server,
                                // You should not use autoAcceptConnections for production
                                // applications, as it defeats all standard cross-origin protection
                                // facilities built into the protocol and the browser.  You should
                                // *always* verify the connection's origin and decide whether or not
                                // to accept it.
                                autoAcceptConnections: false
                            });

                            function originIsAllowed(origin) {
                              // put logic here to detect whether the specified origin is allowed.
                              return true;
                            }
                            var jConnections = new Array();
                            wsServer.on('request', function(request) {

                                if (!originIsAllowed(request.origin)) {
                                  // Make sure we only accept requests from an allowed origin
                                  request.reject();
                                  console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
                                  return;
                                }

                                var connection = request.accept('echo-protocol', request.origin);
                                var jConnectionIdentity = new Date();
                                jConnections.push(new Array(connection,jConnectionIdentity) );
                                //console.log("datestamp"+jConnections[0][1]);
                                console.log((new Date()) + ' Connection accepted.');
                                connection.on('message', function(message) {
                                    if (message.type === 'utf8') {
                                        console.log('Received Message: ' + message.utf8Data);
                                        //connection.sendUTF(message.utf8Data);
                                        for(var i=0;i<jConnections.length;i++)
                                        {
                                            var jConnection = jConnections[i][0];
                                            jConnection.sendUTF(message.utf8Data);
                                        }
                                    }
                                    else if (message.type === 'binary') {
                                        console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
                                        //connection.sendBytes(message.binaryData);
                                        for(var i=0;i<jConnections.length;i++)
                                        {
                                            var jConnection = jConnections[i][0];
                                            jConnection.sendBytes(message.binaryData);
                                        }
                                    }
                                });
                                connection.on('close', function(reasonCode, description) {

                                    for(var i=0;i<jConnections.length;i++)
                                    {   
                                        if(jConnections[i][1] == jConnectionIdentity)
                                        {
                                            delete jConnections[i];
                                            console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
                                            console.log(jConnections.length);
                                        }
                                    }
                                });
                            });

如果我打开3个客户端连接并从每个客户端发送消息然后关闭第三个客户端,我看到“console.log(jConnections.length);”的输出还是三个。在关闭第三个客户端并从客户端2发送消息时,会发生NodeJS崩溃。我的控制台输出如下所示:

                        C:\Program Files\nodejs>node.exe workingexample3.js
                        Mon May 20 2013 16:30:14 GMT-0700 (Pacific Daylight Time) Server is listening on port 8080
                        Mon May 20 2013 16:30:18 GMT-0700 (Pacific Daylight Time) Connection accepted.
                        Mon May 20 2013 16:30:20 GMT-0700 (Pacific Daylight Time) Connection accepted.
                        Mon May 20 2013 16:30:22 GMT-0700 (Pacific Daylight Time) Connection accepted.
                        Received Message: client1
                        Received Message: client2
                        Received Message: client3
                        Mon May 20 2013 16:30:43 GMT-0700 (Pacific Daylight Time) Peer 127.0.0.1 disconnected.
                        3
                        Received Message: client2

                        C:\Program Files\nodejs\workingexample3.js:48
                                                        var jConnection = jConnections[i][0];
                                                                                         ^
                        TypeError: Cannot read property '0' of undefined
                            at WebSocketConnection.<anonymous> (C:\Program Files\nodejs\workingexample3.js:48:38)
                            at WebSocketConnection.EventEmitter.emit (events.js:95:17)
                            at WebSocketConnection.processFrame (C:\Program Files\nodejs\node_modules\websocket\lib\WebSocketConnection.js:403:26)
                            at WebSocketConnection.handleSocketData (C:\Program Files\nodejs\node_modules\websocket\lib\WebSocketConnection.js:247:14)
                            at Socket.EventEmitter.emit (events.js:95:17)
                            at Socket.<anonymous> (_stream_readable.js:736:14)
                            at Socket.EventEmitter.emit (events.js:92:17)
                            at emitReadable_ (_stream_readable.js:408:10)
                            at emitReadable (_stream_readable.js:404:5)
                            at readableAddChunk (_stream_readable.js:165:9)

                        C:\Program Files\nodejs>

我希望在删除语句中jConnections.length的值会减少1,但似乎没有发生。由于我的所有三个客户端都是从l​​ocalhost 127.0.0.1连接的,因此我不得不想出一个计划,通过remoteAdress属性之外的其他东西来识别每个连接。这就是为什么我除了连接对象之外还要推送Date。我希望通过创建它的日期来识别每个连接。

1 个答案:

答案 0 :(得分:2)

1)调用delete后,您的数组看起来像这样 -

[[1, 1], [2, 2], undefined]

长度仍为3:)