File size: 5,440 Bytes
19605ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
/**
* Module dependencies.
*/
var Emitter = require('events').EventEmitter;
/**
* Module exports.
*/
module.exports = Adapter;
/**
* Memory adapter constructor.
*
* @param {Namespace} nsp
* @api public
*/
function Adapter(nsp){
this.nsp = nsp;
this.rooms = {};
this.sids = {};
this.encoder = nsp.server.encoder;
}
/**
* Inherits from `EventEmitter`.
*/
Adapter.prototype.__proto__ = Emitter.prototype;
/**
* Adds a socket to a room.
*
* @param {String} socket id
* @param {String} room name
* @param {Function} callback
* @api public
*/
Adapter.prototype.add = function(id, room, fn){
return this.addAll(id, [ room ], fn);
};
/**
* Adds a socket to a list of room.
*
* @param {String} socket id
* @param {String} rooms
* @param {Function} callback
* @api public
*/
Adapter.prototype.addAll = function(id, rooms, fn){
for (var i = 0; i < rooms.length; i++) {
var room = rooms[i];
this.sids[id] = this.sids[id] || {};
this.sids[id][room] = true;
this.rooms[room] = this.rooms[room] || Room();
this.rooms[room].add(id);
}
if (fn) process.nextTick(fn.bind(null, null));
};
/**
* Removes a socket from a room.
*
* @param {String} socket id
* @param {String} room name
* @param {Function} callback
* @api public
*/
Adapter.prototype.del = function(id, room, fn){
this.sids[id] = this.sids[id] || {};
delete this.sids[id][room];
if (this.rooms.hasOwnProperty(room)) {
this.rooms[room].del(id);
if (this.rooms[room].length === 0) delete this.rooms[room];
}
if (fn) process.nextTick(fn.bind(null, null));
};
/**
* Removes a socket from all rooms it's joined.
*
* @param {String} socket id
* @param {Function} callback
* @api public
*/
Adapter.prototype.delAll = function(id, fn){
var rooms = this.sids[id];
if (rooms) {
for (var room in rooms) {
if (this.rooms.hasOwnProperty(room)) {
this.rooms[room].del(id);
if (this.rooms[room].length === 0) delete this.rooms[room];
}
}
}
delete this.sids[id];
if (fn) process.nextTick(fn.bind(null, null));
};
/**
* Broadcasts a packet.
*
* Options:
* - `flags` {Object} flags for this packet
* - `except` {Array} sids that should be excluded
* - `rooms` {Array} list of rooms to broadcast to
*
* @param {Object} packet object
* @api public
*/
Adapter.prototype.broadcast = function(packet, opts){
var rooms = opts.rooms || [];
var except = opts.except || [];
var flags = opts.flags || {};
var packetOpts = {
preEncoded: true,
volatile: flags.volatile,
compress: flags.compress
};
var ids = {};
var self = this;
var socket;
packet.nsp = this.nsp.name;
this.encoder.encode(packet, function(encodedPackets) {
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] || ~except.indexOf(id)) continue;
socket = self.nsp.connected[id];
if (socket) {
socket.packet(encodedPackets, packetOpts);
ids[id] = true;
}
}
}
}
} else {
for (var id in self.sids) {
if (self.sids.hasOwnProperty(id)) {
if (~except.indexOf(id)) continue;
socket = self.nsp.connected[id];
if (socket) socket.packet(encodedPackets, packetOpts);
}
}
}
});
};
/**
* Gets a list of clients by sid.
*
* @param {Array} explicit set of rooms to check.
* @param {Function} callback
* @api public
*/
Adapter.prototype.clients = function(rooms, fn){
if ('function' == typeof rooms){
fn = rooms;
rooms = null;
}
rooms = rooms || [];
var ids = {};
var sids = [];
var socket;
if (rooms.length) {
for (var i = 0; i < rooms.length; i++) {
var room = this.rooms[rooms[i]];
if (!room) continue;
var sockets = room.sockets;
for (var id in sockets) {
if (sockets.hasOwnProperty(id)) {
if (ids[id]) continue;
socket = this.nsp.connected[id];
if (socket) {
sids.push(id);
ids[id] = true;
}
}
}
}
} else {
for (var id in this.sids) {
if (this.sids.hasOwnProperty(id)) {
socket = this.nsp.connected[id];
if (socket) sids.push(id);
}
}
}
if (fn) process.nextTick(fn.bind(null, null, sids));
};
/**
* Gets the list of rooms a given client has joined.
*
* @param {String} socket id
* @param {Function} callback
* @api public
*/
Adapter.prototype.clientRooms = function(id, fn){
var rooms = this.sids[id];
if (fn) process.nextTick(fn.bind(null, null, rooms ? Object.keys(rooms) : null));
};
/**
* 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--;
}
};
|