arnaucube
arnaucube/raspberryGPIOhtmlserverPublic Self-hosted repository
raspberryGPIOhtmlserver/node_modules/socket.io-adapter/index.js
node_modules/socket.io-adapter/index.jsRaw
  1
  2/**
  3 * Module dependencies.
  4 */
  5
  6var keys = require('object-keys');
  7var Emitter = require('events').EventEmitter;
  8var parser = require('socket.io-parser');
  9
 10/**
 11 * Module exports.
 12 */
 13
 14module.exports = Adapter;
 15
 16/**
 17 * Memory adapter constructor.
 18 *
 19 * @param {Namespace} nsp
 20 * @api public
 21 */
 22
 23function Adapter(nsp){
 24  this.nsp = nsp;
 25  this.rooms = {};
 26  this.sids = {};
 27  this.encoder = new parser.Encoder();
 28}
 29
 30/**
 31 * Inherits from `EventEmitter`.
 32 */
 33
 34Adapter.prototype.__proto__ = Emitter.prototype;
 35
 36/**
 37 * Adds a socket from a room.
 38 *
 39 * @param {String} socket id
 40 * @param {String} room name
 41 * @param {Function} callback
 42 * @api public
 43 */
 44
 45Adapter.prototype.add = function(id, room, fn){
 46  this.sids[id] = this.sids[id] || {};
 47  this.sids[id][room] = true;
 48  this.rooms[room] = this.rooms[room] || {};
 49  this.rooms[room][id] = true;
 50  if (fn) process.nextTick(fn.bind(null, null));
 51};
 52
 53/**
 54 * Removes a socket from a room.
 55 *
 56 * @param {String} socket id
 57 * @param {String} room name
 58 * @param {Function} callback
 59 * @api public
 60 */
 61
 62Adapter.prototype.del = function(id, room, fn){
 63  this.sids[id] = this.sids[id] || {};
 64  this.rooms[room] = this.rooms[room] || {};
 65  delete this.sids[id][room];
 66  delete this.rooms[room][id];
 67  if (this.rooms.hasOwnProperty(room) && !keys(this.rooms[room]).length) {
 68    delete this.rooms[room];
 69  }
 70
 71  if (fn) process.nextTick(fn.bind(null, null));
 72};
 73
 74/**
 75 * Removes a socket from all rooms it's joined.
 76 *
 77 * @param {String} socket id
 78 * @api public
 79 */
 80
 81Adapter.prototype.delAll = function(id, fn){
 82  var rooms = this.sids[id];
 83  if (rooms) {
 84    for (var room in rooms) {
 85      if (rooms.hasOwnProperty(room)) {
 86        delete this.rooms[room][id];
 87      }
 88
 89      if (this.rooms.hasOwnProperty(room) && !keys(this.rooms[room]).length) {
 90        delete this.rooms[room];
 91      }
 92    }
 93  }
 94  delete this.sids[id];
 95};
 96
 97/**
 98 * Broadcasts a packet.
 99 *
100 * Options:
101 *  - `flags` {Object} flags for this packet
102 *  - `except` {Array} sids that should be excluded
103 *  - `rooms` {Array} list of rooms to broadcast to
104 *
105 * @param {Object} packet object
106 * @api public
107 */
108
109Adapter.prototype.broadcast = function(packet, opts){
110  var rooms = opts.rooms || [];
111  var except = opts.except || [];
112  var flags = opts.flags || {};
113  var ids = {};
114  var self = this;
115  var socket;
116
117  packet.nsp = this.nsp.name;
118  this.encoder.encode(packet, function(encodedPackets) {
119    if (rooms.length) {
120      for (var i = 0; i < rooms.length; i++) {
121        var room = self.rooms[rooms[i]];
122        if (!room) continue;
123        for (var id in room) {
124          if (room.hasOwnProperty(id)) {
125            if (ids[id] || ~except.indexOf(id)) continue;
126            socket = self.nsp.connected[id];
127            if (socket) {
128              socket.packet(encodedPackets, true, flags.volatile);
129              ids[id] = true;
130            }
131          }
132        }
133      }
134    } else {
135      for (var id in self.sids) {
136        if (self.sids.hasOwnProperty(id)) {
137          if (~except.indexOf(id)) continue;
138          socket = self.nsp.connected[id];
139          if (socket) socket.packet(encodedPackets, true, flags.volatile);
140        }
141      }
142    }
143  });
144};