Node.js Socket.IO“Uncaught TypeError:Object#<server>没有方法'close'”</server>

时间:2014-07-13 19:33:45

标签: javascript node.js http websocket socket.io

我正在尝试使用mySocketIoInstance.close()关闭与socket.io实例关联的http服务器,但是我收到以下错误:

 Uncaught TypeError: Object #<Server> has no method 'close'
  at Socket.<anonymous> (/home/jackson/projects/tcg/test/server/ServerLobbySpec.js:35:20)
  at Socket.Emitter.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-emitter/index.js:134:20)
  at Socket.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/socket.js:128:10)
  at Socket.onconnect (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/socket.js:306:8)
  at Socket.onpacket (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/socket.js:206:12)
  at Manager.<anonymous> (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-bind/index.js:21:15)
  at Manager.Emitter.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-emitter/index.js:134:20)
  at Manager.ondecoded (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/manager.js:270:8)
  at Decoder.<anonymous> (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-bind/index.js:21:15)
  at Decoder.Emitter.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/socket.io-parser/node_modules/emitter/index.js:132:20)

这是我试图关闭它的地方:

'use strict';

var http = require('http'),
    expect = require('chai').expect,
    socketIo = require('socket.io'),
    socketIoClient = require('socket.io-client'),
    ServerLobby = require('../../source/server/ServerLobby');

describe('ServerLobby', function () {

    var port = 2468,
        uri = 'http://localhost:' + port;

    describe('is connectable', function () {

        it('should connect', function (done) {

            var httpServer = http.Server().listen(port),
                io = socketIo(httpServer),
                lobby = new ServerLobby(io),
                socket = socketIoClient(uri + lobby.namespace);

            socket.on('connect', function () {

                // HERE
                io.close();
                // HERE

                done();
            });

        });

    });

});

我尝试评论lobbysocket代码并console.log'd io.close并获得undefined

我的版本是1.0.6。根据{{​​1}}(也是1.0.6),我应该能够以下列方式关闭服务器:

socket.io/test/socket.io.js

我的设置看起来几乎就是这样。请指教。

1 个答案:

答案 0 :(得分:0)

examples和您包含的测试中,它显示了一个socket.io对象,该对象是使用已以不同方式初始化的服务器对象构建的。

试试这个:

var http = require('http').Server,
// ...
var httpServer = http().listen(port);

在发布时从link获取的完整示例:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});