]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/tidep0084.git/blob - example/iot-gateway/node_modules/socket.io-adapter/index.js
Initial commit
[apps/tidep0084.git] / example / iot-gateway / node_modules / socket.io-adapter / index.js
2 /**
3  * Module dependencies.
4  */
6 var Emitter = require('events').EventEmitter;
7 var parser = require('socket.io-parser');
9 /**
10  * Module exports.
11  */
13 module.exports = Adapter;
15 /**
16  * Memory adapter constructor.
17  *
18  * @param {Namespace} nsp
19  * @api public
20  */
22 function Adapter(nsp){
23   this.nsp = nsp;
24   this.rooms = {};
25   this.sids = {};
26   this.encoder = new parser.Encoder();
27 }
29 /**
30  * Inherits from `EventEmitter`.
31  */
33 Adapter.prototype.__proto__ = Emitter.prototype;
35 /**
36  * Adds a socket to a room.
37  *
38  * @param {String} socket id
39  * @param {String} room name
40  * @param {Function} callback
41  * @api public
42  */
44 Adapter.prototype.add = function(id, room, fn){
45   this.sids[id] = this.sids[id] || {};
46   this.sids[id][room] = true;
47   this.rooms[room] = this.rooms[room] || Room();
48   this.rooms[room].add(id);
49   if (fn) process.nextTick(fn.bind(null, null));
50 };
52 /**
53  * Removes a socket from a room.
54  *
55  * @param {String} socket id
56  * @param {String} room name
57  * @param {Function} callback
58  * @api public
59  */
61 Adapter.prototype.del = function(id, room, fn){
62   this.sids[id] = this.sids[id] || {};
63   delete this.sids[id][room];
64   if (this.rooms.hasOwnProperty(room)) {
65     this.rooms[room].del(id);
66     if (this.rooms[room].length === 0) delete this.rooms[room];
67   }
69   if (fn) process.nextTick(fn.bind(null, null));
70 };
72 /**
73  * Removes a socket from all rooms it's joined.
74  *
75  * @param {String} socket id
76  * @param {Function} callback
77  * @api public
78  */
80 Adapter.prototype.delAll = function(id, fn){
81   var rooms = this.sids[id];
82   if (rooms) {
83     for (var room in rooms) {
84       if (this.rooms.hasOwnProperty(room)) {
85         this.rooms[room].del(id);
86         if (this.rooms[room].length === 0) delete this.rooms[room];
87       }
88     }
89   }
90   delete this.sids[id];
92   if (fn) process.nextTick(fn.bind(null, null));
93 };
95 /**
96  * Broadcasts a packet.
97  *
98  * Options:
99  *  - `flags` {Object} flags for this packet
100  *  - `except` {Array} sids that should be excluded
101  *  - `rooms` {Array} list of rooms to broadcast to
102  *
103  * @param {Object} packet object
104  * @api public
105  */
107 Adapter.prototype.broadcast = function(packet, opts){
108   var rooms = opts.rooms || [];
109   var except = opts.except || [];
110   var flags = opts.flags || {};
111   var packetOpts = {
112     preEncoded: true,
113     volatile: flags.volatile,
114     compress: flags.compress
115   };
116   var ids = {};
117   var self = this;
118   var socket;
120   packet.nsp = this.nsp.name;
121   this.encoder.encode(packet, function(encodedPackets) {
122     if (rooms.length) {
123       for (var i = 0; i < rooms.length; i++) {
124         var room = self.rooms[rooms[i]];
125         if (!room) continue;
126         var sockets = room.sockets;
127         for (var id in sockets) {
128           if (sockets.hasOwnProperty(id)) {
129             if (ids[id] || ~except.indexOf(id)) continue;
130             socket = self.nsp.connected[id];
131             if (socket) {
132               socket.packet(encodedPackets, packetOpts);
133               ids[id] = true;
134             }
135           }
136         }
137       }
138     } else {
139       for (var id in self.sids) {
140         if (self.sids.hasOwnProperty(id)) {
141           if (~except.indexOf(id)) continue;
142           socket = self.nsp.connected[id];
143           if (socket) socket.packet(encodedPackets, packetOpts);
144         }
145       }
146     }
147   });
148 };
150 /**
151  * Gets a list of clients by sid.
152  *
153  * @param {Array} explicit set of rooms to check.
154  * @api public
155  */
157 Adapter.prototype.clients = function(rooms, fn){
158   if ('function' == typeof rooms){
159     fn = rooms;
160     rooms = null;
161   }
163   rooms = rooms || [];
165   var ids = {};
166   var self = this;
167   var sids = [];
168   var socket;
170   if (rooms.length) {
171     for (var i = 0; i < rooms.length; i++) {
172       var room = self.rooms[rooms[i]];
173       if (!room) continue;
174       var sockets = room.sockets;
175       for (var id in sockets) {
176         if (sockets.hasOwnProperty(id)) {
177           if (ids[id]) continue;
178           socket = self.nsp.connected[id];
179           if (socket) {
180             sids.push(id);
181             ids[id] = true;
182           }
183         }
184       }
185     }
186   } else {
187     for (var id in self.sids) {
188       if (self.sids.hasOwnProperty(id)) {
189         socket = self.nsp.connected[id];
190         if (socket) sids.push(id);
191       }
192     }
193   }
195   if (fn) process.nextTick(fn.bind(null, null, sids));
196 };
198 /**
199 * Room constructor.
201 * @api private
202 */
204 function Room(){
205   if (!(this instanceof Room)) return new Room();
206   this.sockets = {};
207   this.length = 0;
210 /**
211  * Adds a socket to a room.
212  *
213  * @param {String} socket id
214  * @api private
215  */
217 Room.prototype.add = function(id){
218   if (!this.sockets.hasOwnProperty(id)) {
219     this.sockets[id] = true;
220     this.length++;
221   }
222 };
224 /**
225  * Removes a socket from a room.
226  *
227  * @param {String} socket id
228  * @api private
229  */
231 Room.prototype.del = function(id){
232   if (this.sockets.hasOwnProperty(id)) {
233     delete this.sockets[id];
234     this.length--;
235   }
236 };