6 var Emitter = require('events').EventEmitter;
12 module.exports = Adapter;
15 * Memory adapter constructor.
17 * @param {Namespace} nsp
21 function Adapter(nsp){
25 this.encoder = nsp.server.encoder;
29 * Inherits from `EventEmitter`.
32 Adapter.prototype.__proto__ = Emitter.prototype;
35 * Adds a socket to a room.
37 * @param {String} socket id
38 * @param {String} room name
39 * @param {Function} callback
43 Adapter.prototype.add = function(id, room, fn){
44 return this.addAll(id, [ room ], fn);
48 * Adds a socket to a list of room.
50 * @param {String} socket id
51 * @param {String} rooms
52 * @param {Function} callback
56 Adapter.prototype.addAll = function(id, rooms, fn){
57 for (var i = 0; i < rooms.length; i++) {
59 this.sids[id] = this.sids[id] || {};
60 this.sids[id][room] = true;
61 this.rooms[room] = this.rooms[room] || Room();
62 this.rooms[room].add(id);
64 if (fn) process.nextTick(fn.bind(null, null));
68 * Removes a socket from a room.
70 * @param {String} socket id
71 * @param {String} room name
72 * @param {Function} callback
76 Adapter.prototype.del = function(id, room, fn){
77 if (this.sids[id]) delete this.sids[id][room];
79 if (this.rooms.hasOwnProperty(room)) {
80 this.rooms[room].del(id);
81 if (this.rooms[room].length === 0) delete this.rooms[room];
84 if (fn) process.nextTick(fn.bind(null, null));
88 * Removes a socket from all rooms it's joined.
90 * @param {String} socket id
91 * @param {Function} callback
95 Adapter.prototype.delAll = function(id, fn){
96 var rooms = this.sids[id];
98 for (var room in rooms) {
99 if (this.rooms.hasOwnProperty(room)) {
100 this.rooms[room].del(id);
101 if (this.rooms[room].length === 0) delete this.rooms[room];
105 delete this.sids[id];
107 if (fn) process.nextTick(fn.bind(null, null));
111 * Broadcasts a packet.
114 * - `flags` {Object} flags for this packet
115 * - `except` {Array} sids that should be excluded
116 * - `rooms` {Array} list of rooms to broadcast to
118 * @param {Object} packet object
122 Adapter.prototype.broadcast = function(packet, opts){
123 var rooms = opts.rooms || [];
124 var except = opts.except || [];
125 var flags = opts.flags || {};
128 volatile: flags.volatile,
129 compress: flags.compress
135 packet.nsp = this.nsp.name;
136 this.encoder.encode(packet, function(encodedPackets) {
138 for (var i = 0; i < rooms.length; i++) {
139 var room = self.rooms[rooms[i]];
141 var sockets = room.sockets;
142 for (var id in sockets) {
143 if (sockets.hasOwnProperty(id)) {
144 if (ids[id] || ~except.indexOf(id)) continue;
145 socket = self.nsp.connected[id];
147 socket.packet(encodedPackets, packetOpts);
154 for (var id in self.sids) {
155 if (self.sids.hasOwnProperty(id)) {
156 if (~except.indexOf(id)) continue;
157 socket = self.nsp.connected[id];
158 if (socket) socket.packet(encodedPackets, packetOpts);
166 * Gets a list of clients by sid.
168 * @param {Array} explicit set of rooms to check.
169 * @param {Function} callback
173 Adapter.prototype.clients = function(rooms, fn){
174 if ('function' == typeof rooms){
186 for (var i = 0; i < rooms.length; i++) {
187 var room = this.rooms[rooms[i]];
189 var sockets = room.sockets;
190 for (var id in sockets) {
191 if (sockets.hasOwnProperty(id)) {
192 if (ids[id]) continue;
193 socket = this.nsp.connected[id];
202 for (var id in this.sids) {
203 if (this.sids.hasOwnProperty(id)) {
204 socket = this.nsp.connected[id];
205 if (socket) sids.push(id);
210 if (fn) process.nextTick(fn.bind(null, null, sids));
214 * Gets the list of rooms a given client has joined.
216 * @param {String} socket id
217 * @param {Function} callback
220 Adapter.prototype.clientRooms = function(id, fn){
221 var rooms = this.sids[id];
222 if (fn) process.nextTick(fn.bind(null, null, rooms ? Object.keys(rooms) : null));
232 if (!(this instanceof Room)) return new Room();
238 * Adds a socket to a room.
240 * @param {String} socket id
244 Room.prototype.add = function(id){
245 if (!this.sockets.hasOwnProperty(id)) {
246 this.sockets[id] = true;
252 * Removes a socket from a room.
254 * @param {String} socket id
258 Room.prototype.del = function(id){
259 if (this.sockets.hasOwnProperty(id)) {
260 delete this.sockets[id];