1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var Socket = require('./socket');
|
7 | var Emitter = require('events').EventEmitter;
|
8 | var parser = require('socket.io-parser');
|
9 | var debug = require('debug')('socket.io:namespace');
|
10 | var hasBin = require('has-binary');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | module.exports = exports = Namespace;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | exports.events = [
|
23 | 'connect',
|
24 | 'connection',
|
25 | 'newListener'
|
26 | ];
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | exports.flags = [
|
33 | 'json',
|
34 | 'volatile'
|
35 | ];
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | var emit = Emitter.prototype.emit;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function Namespace(server, name){
|
52 | this.name = name;
|
53 | this.server = server;
|
54 | this.sockets = {};
|
55 | this.connected = {};
|
56 | this.fns = [];
|
57 | this.ids = 0;
|
58 | this.initAdapter();
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Namespace.prototype.__proto__ = Emitter.prototype;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | exports.flags.forEach(function(flag){
|
72 | Namespace.prototype.__defineGetter__(flag, function(){
|
73 | this.flags = this.flags || {};
|
74 | this.flags[flag] = true;
|
75 | return this;
|
76 | });
|
77 | });
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | Namespace.prototype.initAdapter = function(){
|
88 | this.adapter = new (this.server.adapter())(this);
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | Namespace.prototype.use = function(fn){
|
99 | this.fns.push(fn);
|
100 | return this;
|
101 | };
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | Namespace.prototype.run = function(socket, fn){
|
112 | var fns = this.fns.slice(0);
|
113 | if (!fns.length) return fn(null);
|
114 |
|
115 | function run(i){
|
116 | fns[i](socket, function(err){
|
117 |
|
118 | if (err) return fn(err);
|
119 |
|
120 |
|
121 | if (!fns[i + 1]) return fn(null);
|
122 |
|
123 |
|
124 | run(i + 1);
|
125 | });
|
126 | }
|
127 |
|
128 | run(0);
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | Namespace.prototype.to =
|
140 | Namespace.prototype['in'] = function(name){
|
141 | this.rooms = this.rooms || [];
|
142 | if (!~this.rooms.indexOf(name)) this.rooms.push(name);
|
143 | return this;
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | Namespace.prototype.add = function(client, fn){
|
154 | debug('adding socket to nsp %s', this.name);
|
155 | var socket = new Socket(this, client);
|
156 | var self = this;
|
157 | this.run(socket, function(err){
|
158 | process.nextTick(function(){
|
159 | if ('open' == client.conn.readyState) {
|
160 | if (err) return socket.error(err.data || err.message);
|
161 |
|
162 |
|
163 | self.sockets[socket.id] = socket;
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | socket.onconnect();
|
170 | if (fn) fn();
|
171 |
|
172 |
|
173 | self.emit('connect', socket);
|
174 | self.emit('connection', socket);
|
175 | } else {
|
176 | debug('next called after client was closed - ignoring socket');
|
177 | }
|
178 | });
|
179 | });
|
180 | return socket;
|
181 | };
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | Namespace.prototype.remove = function(socket){
|
190 | if (this.sockets.hasOwnProperty(socket.id)) {
|
191 | delete this.sockets[socket.id];
|
192 | } else {
|
193 | debug('ignoring remove for %s', socket.id);
|
194 | }
|
195 | };
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | Namespace.prototype.emit = function(ev){
|
205 | if (~exports.events.indexOf(ev)) {
|
206 | emit.apply(this, arguments);
|
207 | } else {
|
208 |
|
209 | var args = Array.prototype.slice.call(arguments);
|
210 | var parserType = parser.EVENT;
|
211 | if (hasBin(args)) { parserType = parser.BINARY_EVENT; }
|
212 |
|
213 | var packet = { type: parserType, data: args };
|
214 |
|
215 | if ('function' == typeof args[args.length - 1]) {
|
216 | throw new Error('Callbacks are not supported when broadcasting');
|
217 | }
|
218 |
|
219 | this.adapter.broadcast(packet, {
|
220 | rooms: this.rooms,
|
221 | flags: this.flags
|
222 | });
|
223 |
|
224 | delete this.rooms;
|
225 | delete this.flags;
|
226 | }
|
227 | return this;
|
228 | };
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | Namespace.prototype.send =
|
238 | Namespace.prototype.write = function(){
|
239 | var args = Array.prototype.slice.call(arguments);
|
240 | args.unshift('message');
|
241 | this.emit.apply(this, args);
|
242 | return this;
|
243 | };
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | Namespace.prototype.clients = function(fn){
|
253 | this.adapter.clients(this.rooms, fn);
|
254 |
|
255 |
|
256 | delete this.rooms;
|
257 | return this;
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | Namespace.prototype.compress = function(compress){
|
269 | this.flags = this.flags || {};
|
270 | this.flags.compress = compress;
|
271 | return this;
|
272 | };
|