Skip to content

Commit a1c38a2

Browse files
converted arrays to objects
1 parent ae5420b commit a1c38a2

File tree

5 files changed

+40
-47
lines changed

5 files changed

+40
-47
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
}
@@ -72,7 +72,7 @@ Client.prototype.connect = function(name){
7272

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

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

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

@@ -105,10 +103,9 @@ Client.prototype.disconnect = function(){
105103
*/
106104

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

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

@@ -225,10 +222,10 @@ Client.prototype.onclose = function(reason){
225222
this.destroy();
226223

227224
// `nsps` and `sockets` are cleaned up seamlessly
228-
var socket;
229-
while (socket = this.sockets.shift()) {
230-
socket.onclose(reason);
225+
for (var id in this.sockets) {
226+
this.sockets[id].onclose(reason);
231227
}
228+
this.sockets = {};
232229

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

lib/index.js

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

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

353353
this.engine.close();
354354

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;
@@ -161,7 +161,7 @@ Namespace.prototype.add = function(client, fn){
161161
if (err) return socket.error(err.data || err.message);
162162

163163
// track socket
164-
self.sockets.push(socket);
164+
self.sockets[socket.id] = socket;
165165

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

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

lib/socket.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ function Socket(nsp, client){
5959
this.nsp = nsp;
6060
this.server = nsp.server;
6161
this.adapter = this.nsp.adapter;
62-
this.id = client.id;
62+
this.id = nsp.name + '#' + client.id;
6363
this.request = client.request;
6464
this.client = client;
6565
this.conn = client.conn;
66-
this.rooms = [];
66+
this.rooms = {};
6767
this.acks = {};
6868
this.connected = true;
6969
this.disconnected = false;
@@ -223,11 +223,11 @@ Socket.prototype.packet = function(packet, opts){
223223
Socket.prototype.join = function(room, fn){
224224
debug('joining room %s', room);
225225
var self = this;
226-
if (~this.rooms.indexOf(room)) return this;
226+
if (this.rooms[room]) return this;
227227
this.adapter.add(this.id, room, function(err){
228228
if (err) return fn && fn(err);
229229
debug('joined room %s', room);
230-
self.rooms.push(room);
230+
self.rooms[room] = room;
231231
fn && fn(null);
232232
});
233233
return this;
@@ -248,10 +248,7 @@ Socket.prototype.leave = function(room, fn){
248248
this.adapter.del(this.id, room, function(err){
249249
if (err) return fn && fn(err);
250250
debug('left room %s', room);
251-
var idx = self.rooms.indexOf(room);
252-
if (idx >= 0) {
253-
self.rooms.splice(idx, 1);
254-
}
251+
delete self.rooms[room];
255252
fn && fn(null);
256253
});
257254
return this;
@@ -265,7 +262,7 @@ Socket.prototype.leave = function(room, fn){
265262

266263
Socket.prototype.leaveAll = function(){
267264
this.adapter.delAll(this.id);
268-
this.rooms = [];
265+
this.rooms = {};
269266
};
270267

271268
/**

test/socket.io.js

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

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

349349
clientSocket.on('connect', function init() {
350-
expect(sio.nsps['/'].sockets.length).to.equal(1);
350+
expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
351351
sio.close();
352352
});
353353

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

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

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

@@ -1906,15 +1906,15 @@ describe('socket.io', function(){
19061906
var socket = client(srv);
19071907
sio.on('connection', function(s){
19081908
s.join('a', function(){
1909-
expect(s.rooms).to.eql([s.id, 'a']);
1909+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
19101910
s.join('b', function(){
1911-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1911+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
19121912
s.join( 'c', function(){
1913-
expect(s.rooms).to.eql([s.id, 'a', 'b', 'c']);
1913+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b', 'c']);
19141914
s.leave('b', function(){
1915-
expect(s.rooms).to.eql([s.id, 'a', 'c']);
1915+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'c']);
19161916
s.leaveAll();
1917-
expect(s.rooms).to.eql([]);
1917+
expect(Object.keys(s.rooms)).to.eql([]);
19181918
done();
19191919
});
19201920
});
@@ -1950,13 +1950,13 @@ describe('socket.io', function(){
19501950
var socket = client(srv);
19511951
sio.on('connection', function(s){
19521952
s.join('a', function(){
1953-
expect(s.rooms).to.eql([s.id, 'a']);
1953+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
19541954
s.join('b', function(){
1955-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1955+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
19561956
s.leave('unknown', function(){
1957-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1957+
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
19581958
s.leaveAll();
1959-
expect(s.rooms).to.eql([]);
1959+
expect(Object.keys(s.rooms)).to.eql([]);
19601960
done();
19611961
});
19621962
});

0 commit comments

Comments
 (0)