Skip to content

Commit 8683206

Browse files
committed
Merge pull request #2239 from darrachequesne/issue-2199
converted arrays to objects
2 parents 988d9d2 + b73d9be commit 8683206

File tree

5 files changed

+40
-46
lines changed

5 files changed

+40
-46
lines changed

lib/client.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Client(server, conn){
2828
this.id = conn.id;
2929
this.request = conn.request;
3030
this.setup();
31-
this.sockets = [];
31+
this.sockets = {};
3232
this.nsps = {};
3333
this.connectBuffer = [];
3434
}
@@ -73,7 +73,7 @@ Client.prototype.connect = function(name){
7373

7474
var self = this;
7575
var socket = nsp.add(this, function(){
76-
self.sockets.push(socket);
76+
self.sockets[socket.id] = socket;
7777
self.nsps[nsp.name] = socket;
7878

7979
if ('/' == nsp.name && self.connectBuffer.length > 0) {
@@ -90,12 +90,10 @@ Client.prototype.connect = function(name){
9090
*/
9191

9292
Client.prototype.disconnect = function(){
93-
var socket;
94-
// we don't use a for loop because the length of
95-
// `sockets` changes upon each iteration
96-
while (socket = this.sockets.shift()) {
97-
socket.disconnect();
93+
for (var id in this.sockets) {
94+
this.sockets[id].disconnect();
9895
}
96+
this.sockets = {};
9997
this.close();
10098
};
10199

@@ -106,10 +104,9 @@ Client.prototype.disconnect = function(){
106104
*/
107105

108106
Client.prototype.remove = function(socket){
109-
var i = this.sockets.indexOf(socket);
110-
if (~i) {
111-
var nsp = this.sockets[i].nsp.name;
112-
this.sockets.splice(i, 1);
107+
if (this.sockets[socket.id]) {
108+
var nsp = this.sockets[socket.id].nsp.name;
109+
delete this.sockets[socket.id];
113110
delete this.nsps[nsp];
114111
} else {
115112
debug('ignoring remove for %s', socket.id);
@@ -206,9 +203,9 @@ Client.prototype.ondecoded = function(packet) {
206203
*/
207204

208205
Client.prototype.onerror = function(err){
209-
this.sockets.forEach(function(socket){
210-
socket.onerror(err);
211-
});
206+
for (var id in this.sockets) {
207+
this.sockets[id].onerror(err);
208+
}
212209
this.onclose('client error');
213210
};
214211

@@ -226,10 +223,10 @@ Client.prototype.onclose = function(reason){
226223
this.destroy();
227224

228225
// `nsps` and `sockets` are cleaned up seamlessly
229-
var socket;
230-
while (socket = this.sockets.shift()) {
231-
socket.onclose(reason);
226+
for (var id in this.sockets) {
227+
this.sockets[id].onclose(reason);
232228
}
229+
this.sockets = {};
233230

234231
this.decoder.destroy(); // clean up decoder
235232
};

lib/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ Server.prototype.of = function(name, fn){
347347
*/
348348

349349
Server.prototype.close = function(){
350-
this.nsps['/'].sockets.forEach(function(socket){
351-
socket.onclose();
352-
});
350+
for (var id in this.nsps['/'].sockets) {
351+
this.nsps['/'].sockets[id].onclose();
352+
}
353353

354354
this.engine.close();
355355

lib/namespace.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var emit = Emitter.prototype.emit;
5151
function Namespace(server, name){
5252
this.name = name;
5353
this.server = server;
54-
this.sockets = [];
54+
this.sockets = {};
5555
this.connected = {};
5656
this.fns = [];
5757
this.ids = 0;
@@ -160,7 +160,7 @@ Namespace.prototype.add = function(client, fn){
160160
if (err) return socket.error(err.data || err.message);
161161

162162
// track socket
163-
self.sockets.push(socket);
163+
self.sockets[socket.id] = socket;
164164

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

189189
Namespace.prototype.remove = function(socket){
190-
var i = this.sockets.indexOf(socket);
191-
if (~i) {
192-
this.sockets.splice(i, 1);
190+
if (this.sockets[socket.id]) {
191+
delete this.sockets[socket.id];
193192
} else {
194193
debug('ignoring remove for %s', socket.id);
195194
}

lib/socket.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ function Socket(nsp, client){
6060
this.server = nsp.server;
6161
this.adapter = this.nsp.adapter;
6262
this.id = client.id;
63+
this.id = nsp.name + '#' + client.id;
6364
this.client = client;
6465
this.conn = client.conn;
65-
this.rooms = [];
66+
this.rooms = {};
6667
this.acks = {};
6768
this.connected = true;
6869
this.disconnected = false;
@@ -222,11 +223,11 @@ Socket.prototype.packet = function(packet, opts){
222223
Socket.prototype.join = function(room, fn){
223224
debug('joining room %s', room);
224225
var self = this;
225-
if (~this.rooms.indexOf(room)) return this;
226+
if (this.rooms[room]) return this;
226227
this.adapter.add(this.id, room, function(err){
227228
if (err) return fn && fn(err);
228229
debug('joined room %s', room);
229-
self.rooms.push(room);
230+
self.rooms[room] = room;
230231
fn && fn(null);
231232
});
232233
return this;
@@ -247,10 +248,7 @@ Socket.prototype.leave = function(room, fn){
247248
this.adapter.del(this.id, room, function(err){
248249
if (err) return fn && fn(err);
249250
debug('left room %s', room);
250-
var idx = self.rooms.indexOf(room);
251-
if (idx >= 0) {
252-
self.rooms.splice(idx, 1);
253-
}
251+
delete self.rooms[room];
254252
fn && fn(null);
255253
});
256254
return this;
@@ -264,7 +262,7 @@ Socket.prototype.leave = function(room, fn){
264262

265263
Socket.prototype.leaveAll = function(){
266264
this.adapter.delAll(this.id);
267-
this.rooms = [];
265+
this.rooms = {};
268266
};
269267

270268
/**

test/socket.io.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,12 @@ describe('socket.io', function(){
368368
var clientSocket = client(srv, { reconnection: false });
369369

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

375375
clientSocket.on('connect', function init() {
376-
expect(sio.nsps['/'].sockets.length).to.equal(1);
376+
expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
377377
sio.close();
378378
});
379379

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

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

402402
clientSocket.on('connect', function init() {
403-
expect(sio.nsps['/'].sockets.length).to.equal(1);
403+
expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
404404
sio.close();
405405
});
406406

@@ -1932,15 +1932,15 @@ describe('socket.io', function(){
19321932
var socket = client(srv);
19331933
sio.on('connection', function(s){
19341934
s.join('a', function(){
1935-
expect(s.rooms).to.eql([s.id, 'a']);
1935+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
19361936
s.join('b', function(){
1937-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1937+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
19381938
s.join( 'c', function(){
1939-
expect(s.rooms).to.eql([s.id, 'a', 'b', 'c']);
1939+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b', 'c']);
19401940
s.leave('b', function(){
1941-
expect(s.rooms).to.eql([s.id, 'a', 'c']);
1941+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'c']);
19421942
s.leaveAll();
1943-
expect(s.rooms).to.eql([]);
1943+
expect(Object.keys(s.rooms)).to.eql([]);
19441944
done();
19451945
});
19461946
});
@@ -1976,13 +1976,13 @@ describe('socket.io', function(){
19761976
var socket = client(srv);
19771977
sio.on('connection', function(s){
19781978
s.join('a', function(){
1979-
expect(s.rooms).to.eql([s.id, 'a']);
1979+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
19801980
s.join('b', function(){
1981-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1981+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
19821982
s.leave('unknown', function(){
1983-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1983+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
19841984
s.leaveAll();
1985-
expect(s.rooms).to.eql([]);
1985+
expect(Object.keys(s.rooms)).to.eql([]);
19861986
done();
19871987
});
19881988
});

0 commit comments

Comments
 (0)