[英]Node.js Socket.IO “Uncaught TypeError: Object #<Server> has no method 'close'”
I am trying to close a http server associated with a socket.io instance, using mySocketIoInstance.close()
, but I am getting the following error: 我正在尝试使用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)
Here's where I'm trying to close it: 这是我要关闭的地方:
'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();
});
});
});
});
I tried commenting-out the lobby
and socket
code and just console.log
'd io.close
and got undefined
. 我试图注释掉lobby
和socket
代码,只是console.log
io.close
并得到undefined
。
My version is 1.0.6. 我的版本是1.0.6。 According to socket.io/test/socket.io.js
(also 1.0.6), I should be able to close the server in the following way: 根据socket.io/test/socket.io.js
(也是1.0.6),我应该能够通过以下方式关闭服务器:
var http = require('http').Server;
var io = require('..');
var expect = require('expect.js');
// ...
it('should be able to close sio sending a srv', function() {
var PORT = 54016;
var srv = http().listen(PORT);
var sio = io(srv);
var clientSocket = client(srv, {
reconnection: false
});
clientSocket.on('connect', function init() {
expect(sio.nsps['/'].sockets.length).to.equal(1);
// HERE
sio.close();
// HERE
});
});
My setup looks almost exactly like that. 我的设置几乎完全像这样。 Please advise. 请指教。
In the examples and in the test you have included, it shows a socket.io object being constructed with a server object that has been initialized in a different manner. 在示例和包含的测试中,它显示了一个socket.io对象,该对象正在使用以不同方式初始化的服务器对象构造。
Try this: 尝试这个:
var http = require('http').Server,
// ...
var httpServer = http().listen(port);
Full example taken from link at time of posting: 发布时链接的完整示例:
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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.