index.js 5.3 KB

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