I am trying to close a http server associated with a socket.io instance, using mySocketIoInstance.close()
, but I am getting the following error:
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
.
My version is 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:
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.
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);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.