Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Client.prototype.disconnect = function(){
var socket;
// we don't use a for loop because the length of
// `sockets` changes upon each iteration
while (socket = this.sockets.shift()) {
while (socket = this.sockets.pop()) {
socket.disconnect();
}
this.close();
Expand Down Expand Up @@ -226,7 +226,7 @@ Client.prototype.onclose = function(reason){

// `nsps` and `sockets` are cleaned up seamlessly
var socket;
while (socket = this.sockets.shift()) {
while (socket = this.sockets.pop()) {
socket.onclose(reason);
}

Expand Down
11 changes: 7 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Server.prototype.onconnection = function(conn){

Server.prototype.of = function(name, fn){
if (String(name)[0] !== '/') name = '/' + name;

if (!this.nsps[name]) {
debug('initializing namespace %s', name);
var nsp = new Namespace(this, name);
Expand All @@ -346,9 +346,12 @@ Server.prototype.of = function(name, fn){
*/

Server.prototype.close = function(){
this.nsps['/'].sockets.forEach(function(socket){
socket.onclose();
});
var sockets = this.nsps['/'].sockets;
for (var id in sockets) {
if (sockets.hasOwnProperty(id)) {
sockets[id].onclose();
}
}

this.engine.close();

Expand Down
9 changes: 4 additions & 5 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var emit = Emitter.prototype.emit;
function Namespace(server, name){
this.name = name;
this.server = server;
this.sockets = [];
this.sockets = {};
this.connected = {};
this.fns = [];
this.ids = 0;
Expand Down Expand Up @@ -161,7 +161,7 @@ Namespace.prototype.add = function(client, fn){
if (err) return socket.error(err.data || err.message);

// track socket
self.sockets.push(socket);
self.sockets[socket.id] = socket;

// it's paramount that the internal `onconnect` logic
// fires before user-set events to prevent state order
Expand All @@ -188,9 +188,8 @@ Namespace.prototype.add = function(client, fn){
*/

Namespace.prototype.remove = function(socket){
var i = this.sockets.indexOf(socket);
if (~i) {
this.sockets.splice(i, 1);
if (this.sockets.hasOwnProperty(socket.id)) {
delete this.sockets[socket.id];
} else {
debug('ignoring remove for %s', socket.id);
}
Expand Down
10 changes: 5 additions & 5 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ describe('socket.io', function(){
var clientSocket = client(srv, { reconnection: false });

clientSocket.on('disconnect', function init() {
expect(sio.nsps['/'].sockets.length).to.equal(0);
expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0);
server.listen(PORT);
});

clientSocket.on('connect', function init() {
expect(sio.nsps['/'].sockets.length).to.equal(1);
expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
sio.close();
});

Expand All @@ -369,12 +369,12 @@ describe('socket.io', function(){
var clientSocket = ioc('ws://0.0.0.0:' + PORT);

clientSocket.on('disconnect', function init() {
expect(sio.nsps['/'].sockets.length).to.equal(0);
expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0);
server.listen(PORT);
});

clientSocket.on('connect', function init() {
expect(sio.nsps['/'].sockets.length).to.equal(1);
expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
sio.close();
});

Expand Down Expand Up @@ -603,7 +603,7 @@ describe('socket.io', function(){
});
});
});

it('should not reuse same-namespace connections', function(done){
var srv = http();
var sio = io(srv);
Expand Down