socket.io-adapter
Advanced tools
Comparing version 0.3.1 to 0.4.0
0.4.0 / 2015-12-03 | ||
================== | ||
* package: bump `debug` | ||
* use a `Room` class to efficiently track room size | ||
* allow `clients(fn)` | ||
* call the callback on `delAll` | ||
0.3.1 / 2014-10-27 | ||
@@ -3,0 +11,0 @@ ================== |
128
index.js
@@ -6,3 +6,2 @@ | ||
var keys = require('object-keys'); | ||
var Emitter = require('events').EventEmitter; | ||
@@ -38,3 +37,3 @@ var parser = require('socket.io-parser'); | ||
/** | ||
* Adds a socket from a room. | ||
* Adds a socket to a room. | ||
* | ||
@@ -50,4 +49,4 @@ * @param {String} socket id | ||
this.sids[id][room] = true; | ||
this.rooms[room] = this.rooms[room] || {}; | ||
this.rooms[room][id] = true; | ||
this.rooms[room] = this.rooms[room] || Room(); | ||
this.rooms[room].add(id); | ||
if (fn) process.nextTick(fn.bind(null, null)); | ||
@@ -67,7 +66,6 @@ }; | ||
this.sids[id] = this.sids[id] || {}; | ||
this.rooms[room] = this.rooms[room] || {}; | ||
delete this.sids[id][room]; | ||
delete this.rooms[room][id]; | ||
if (this.rooms.hasOwnProperty(room) && !keys(this.rooms[room]).length) { | ||
delete this.rooms[room]; | ||
if (this.rooms.hasOwnProperty(room)) { | ||
this.rooms[room].del(id); | ||
if (this.rooms[room].length === 0) delete this.rooms[room]; | ||
} | ||
@@ -82,2 +80,3 @@ | ||
* @param {String} socket id | ||
* @param {Function} callback | ||
* @api public | ||
@@ -90,12 +89,11 @@ */ | ||
for (var room in rooms) { | ||
if (rooms.hasOwnProperty(room)) { | ||
delete this.rooms[room][id]; | ||
if (this.rooms.hasOwnProperty(room)) { | ||
this.rooms[room].del(id); | ||
if (this.rooms[room].length === 0) delete this.rooms[room]; | ||
} | ||
if (this.rooms.hasOwnProperty(room) && !keys(this.rooms[room]).length) { | ||
delete this.rooms[room]; | ||
} | ||
} | ||
} | ||
delete this.sids[id]; | ||
if (fn) process.nextTick(fn.bind(null, null)); | ||
}; | ||
@@ -119,2 +117,7 @@ | ||
var flags = opts.flags || {}; | ||
var packetOpts = { | ||
preEncoded: true, | ||
volatile: flags.volatile, | ||
compress: flags.compress | ||
}; | ||
var ids = {}; | ||
@@ -130,8 +133,9 @@ var self = this; | ||
if (!room) continue; | ||
for (var id in room) { | ||
if (room.hasOwnProperty(id)) { | ||
var sockets = room.sockets; | ||
for (var id in sockets) { | ||
if (sockets.hasOwnProperty(id)) { | ||
if (ids[id] || ~except.indexOf(id)) continue; | ||
socket = self.nsp.connected[id]; | ||
if (socket) { | ||
socket.packet(encodedPackets, true, flags.volatile); | ||
socket.packet(encodedPackets, packetOpts); | ||
ids[id] = true; | ||
@@ -147,3 +151,3 @@ } | ||
socket = self.nsp.connected[id]; | ||
if (socket) socket.packet(encodedPackets, true, flags.volatile); | ||
if (socket) socket.packet(encodedPackets, packetOpts); | ||
} | ||
@@ -154,1 +158,89 @@ } | ||
}; | ||
/** | ||
* Gets a list of clients by sid. | ||
* | ||
* @param {Array} explicit set of rooms to check. | ||
* @api public | ||
*/ | ||
Adapter.prototype.clients = function(rooms, fn){ | ||
if ('function' == typeof rooms){ | ||
fn = rooms; | ||
rooms = null; | ||
} | ||
rooms = rooms || []; | ||
var ids = {}; | ||
var self = this; | ||
var sids = []; | ||
var socket; | ||
if (rooms.length) { | ||
for (var i = 0; i < rooms.length; i++) { | ||
var room = self.rooms[rooms[i]]; | ||
if (!room) continue; | ||
var sockets = room.sockets; | ||
for (var id in sockets) { | ||
if (sockets.hasOwnProperty(id)) { | ||
if (ids[id]) continue; | ||
socket = self.nsp.connected[id]; | ||
if (socket) { | ||
sids.push(id); | ||
ids[id] = true; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
for (var id in self.sids) { | ||
if (self.sids.hasOwnProperty(id)) { | ||
socket = self.nsp.connected[id]; | ||
if (socket) sids.push(id); | ||
} | ||
} | ||
} | ||
if (fn) process.nextTick(fn.bind(null, null, sids)); | ||
}; | ||
/** | ||
* Room constructor. | ||
* | ||
* @api private | ||
*/ | ||
function Room(){ | ||
if (!(this instanceof Room)) return new Room(); | ||
this.sockets = {}; | ||
this.length = 0; | ||
} | ||
/** | ||
* Adds a socket to a room. | ||
* | ||
* @param {String} socket id | ||
* @api private | ||
*/ | ||
Room.prototype.add = function(id){ | ||
if (!this.sockets.hasOwnProperty(id)) { | ||
this.sockets[id] = true; | ||
this.length++; | ||
} | ||
}; | ||
/** | ||
* Removes a socket from a room. | ||
* | ||
* @param {String} socket id | ||
* @api private | ||
*/ | ||
Room.prototype.del = function(id){ | ||
if (this.sockets.hasOwnProperty(id)) { | ||
delete this.sockets[id]; | ||
this.length--; | ||
} | ||
}; |
{ | ||
"name": "socket.io-adapter", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"license": "MIT", | ||
"repository": { | ||
@@ -10,6 +11,5 @@ "type": "git", | ||
"dependencies": { | ||
"debug": "1.0.2", | ||
"socket.io-parser": "2.2.2", | ||
"object-keys": "1.0.1" | ||
"debug": "2.2.0", | ||
"socket.io-parser": "2.2.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
7238
2
6
0
205
+ Addeddebug@2.2.0(transitive)
+ Addedms@0.7.1(transitive)
- Removedobject-keys@1.0.1
- Removeddebug@1.0.2(transitive)
- Removedms@0.6.2(transitive)
- Removedobject-keys@1.0.1(transitive)
Updateddebug@2.2.0