File size: 3,231 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

/*
 * Dependencies
 */

var Namespace = require('./namespace');


/*
 * Room Constructor
 */

var Room = function Room (id) {
  this.id = id;
  this.sockets = [];
  this._namespaces = {};
};


/*
 * (Static) Rooms
 * Holds all the rooms in existance
 */

Room.rooms = {};


/*
 * (Static) Get
 * Get or create a room
 *
 * - id (string)
 * > room
 */

Room.get = function get (id) {
  var room = Room.rooms[id];
  if (! room) {
    room = Room.rooms[id] = new Room(id);
  }
  return room;
};


/*
 * (Static) Flush
 * Remove all the rooms
 */

Room.flush = function flush () {
  for (var id in Room.rooms) {
    Room.get(id).empty();
    delete Room.rooms[id];
  }
};


/*
 * (Private) Join
 * Add a socket to the room
 *
 * - socket (socket)
 */

Room.prototype._join = function _join (socket) {
  if (this.sockets.indexOf(socket) < 0) {
    this.sockets.push(socket);
  }
  return this;
};


/*
 * (Private) Leave
 * Remove a socket from the room
 *
 * - socket (socket)
 */

Room.prototype._leave = function _leave (socket) {
  var index = this.sockets.indexOf(socket);
  if (index >= 0) {
    this.sockets.splice(index, 1);
  }
  return this;
};


/*
 * In
 * Get another room of sockets
 * So you can do `Jandal.sockets.in('room').emit('hi');`
 *
 * - id (int) : id of the room
 * > room
 */

Room.prototype.in = function in_ (id) {
  return Room.get(id);
};


/*
 * Length
 * The number of sockets in the room
 *
 * > int
 */

Room.prototype.length = function length () {
  return this.sockets.length;
};


/*
 * Emit
 * Emit an event to everyone in the room
 *
 * - event (string)
 * - args... (mixed)
 */

Room.prototype.emit = function emit (event, arg1, arg2, arg3) {
  for (var i = 0, len = this.sockets.length; i < len; i++) {
    this.sockets[i].emit(event, arg1, arg2, arg3);
  }
  return this;
};


/*
 * Broadcast
 * Emit events to everyone but the sender
 *
 * - sender (socket)
 * - event (string)
 * - args... (mixed)
 */

Room.prototype.broadcast = function broadcast (sender, event, arg1, arg2, arg3) {
  for (var i = 0, len = this.sockets.length; i < len; i++) {
    var socket = this.sockets[i];
    if (socket.id !== sender) {
      socket.emit(event, arg1, arg2, arg3);
    }
  }
  return this;
};


/*
 * Namespace
 * Get (and maybe create) a namespace for this room
 *
 * - name (string) : name of the namespace
 * > namespace
 */

Room.prototype.namespace = function namespace (name) {
  var ns = this._namespaces[name];
  ns = ns ? ns : this._namespaces[name] = new Namespace(name, this);
  return ns;
};


/*
 * Contains
 * Checks if a socket is already in a room
 *
 * - socket (socket)
 * > boolean
 */

Room.prototype.contains = function contains (socket) {
  for (var i = 0, len = this.sockets.length; i < len; i++) {
    if (this.sockets[i] === socket) {
      return true;
    }
  }
  return false;
};


/*
 * Destroy
 * Remove all sockets from a room
 */

Room.prototype.empty = function empty () {
  for (var i = this.sockets.length - 1; i >= 0; i--) {
    this._leave(this.sockets[i]);
  }
};


/*
 * Release
 * Empty the room and delete it from Room.rooms
 */

Room.prototype.release = function () {
  this.empty();
  delete Room.rooms[this.id];
};




module.exports = Room;