1 | "use strict";
|
---|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
---|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
---|
4 | };
|
---|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
6 | exports.Namespace = void 0;
|
---|
7 | const socket_1 = require("./socket");
|
---|
8 | const events_1 = require("events");
|
---|
9 | const socket_io_parser_1 = require("socket.io-parser");
|
---|
10 | const debug_1 = __importDefault(require("debug"));
|
---|
11 | const debug = debug_1.default("socket.io:namespace");
|
---|
12 | class Namespace extends events_1.EventEmitter {
|
---|
13 | /**
|
---|
14 | * Namespace constructor.
|
---|
15 | *
|
---|
16 | * @param server instance
|
---|
17 | * @param name
|
---|
18 | */
|
---|
19 | constructor(server, name) {
|
---|
20 | super();
|
---|
21 | this.sockets = new Map();
|
---|
22 | /** @private */
|
---|
23 | this._fns = [];
|
---|
24 | /** @private */
|
---|
25 | this._rooms = new Set();
|
---|
26 | /** @private */
|
---|
27 | this._flags = {};
|
---|
28 | /** @private */
|
---|
29 | this._ids = 0;
|
---|
30 | this.server = server;
|
---|
31 | this.name = name;
|
---|
32 | this._initAdapter();
|
---|
33 | }
|
---|
34 | /**
|
---|
35 | * Initializes the `Adapter` for this nsp.
|
---|
36 | * Run upon changing adapter by `Server#adapter`
|
---|
37 | * in addition to the constructor.
|
---|
38 | *
|
---|
39 | * @private
|
---|
40 | */
|
---|
41 | _initAdapter() {
|
---|
42 | this.adapter = new (this.server.adapter())(this);
|
---|
43 | }
|
---|
44 | /**
|
---|
45 | * Sets up namespace middleware.
|
---|
46 | *
|
---|
47 | * @return self
|
---|
48 | * @public
|
---|
49 | */
|
---|
50 | use(fn) {
|
---|
51 | this._fns.push(fn);
|
---|
52 | return this;
|
---|
53 | }
|
---|
54 | /**
|
---|
55 | * Executes the middleware for an incoming client.
|
---|
56 | *
|
---|
57 | * @param socket - the socket that will get added
|
---|
58 | * @param fn - last fn call in the middleware
|
---|
59 | * @private
|
---|
60 | */
|
---|
61 | run(socket, fn) {
|
---|
62 | const fns = this._fns.slice(0);
|
---|
63 | if (!fns.length)
|
---|
64 | return fn(null);
|
---|
65 | function run(i) {
|
---|
66 | fns[i](socket, function (err) {
|
---|
67 | // upon error, short-circuit
|
---|
68 | if (err)
|
---|
69 | return fn(err);
|
---|
70 | // if no middleware left, summon callback
|
---|
71 | if (!fns[i + 1])
|
---|
72 | return fn(null);
|
---|
73 | // go on to next
|
---|
74 | run(i + 1);
|
---|
75 | });
|
---|
76 | }
|
---|
77 | run(0);
|
---|
78 | }
|
---|
79 | /**
|
---|
80 | * Targets a room when emitting.
|
---|
81 | *
|
---|
82 | * @param name
|
---|
83 | * @return self
|
---|
84 | * @public
|
---|
85 | */
|
---|
86 | to(name) {
|
---|
87 | this._rooms.add(name);
|
---|
88 | return this;
|
---|
89 | }
|
---|
90 | /**
|
---|
91 | * Targets a room when emitting.
|
---|
92 | *
|
---|
93 | * @param name
|
---|
94 | * @return self
|
---|
95 | * @public
|
---|
96 | */
|
---|
97 | in(name) {
|
---|
98 | this._rooms.add(name);
|
---|
99 | return this;
|
---|
100 | }
|
---|
101 | /**
|
---|
102 | * Adds a new client.
|
---|
103 | *
|
---|
104 | * @return {Socket}
|
---|
105 | * @private
|
---|
106 | */
|
---|
107 | _add(client, query, fn) {
|
---|
108 | debug("adding socket to nsp %s", this.name);
|
---|
109 | const socket = new socket_1.Socket(this, client, query);
|
---|
110 | this.run(socket, (err) => {
|
---|
111 | process.nextTick(() => {
|
---|
112 | if ("open" == client.conn.readyState) {
|
---|
113 | if (err) {
|
---|
114 | if (client.conn.protocol === 3) {
|
---|
115 | return socket._error(err.data || err.message);
|
---|
116 | }
|
---|
117 | else {
|
---|
118 | return socket._error({
|
---|
119 | message: err.message,
|
---|
120 | data: err.data,
|
---|
121 | });
|
---|
122 | }
|
---|
123 | }
|
---|
124 | // track socket
|
---|
125 | this.sockets.set(socket.id, socket);
|
---|
126 | // it's paramount that the internal `onconnect` logic
|
---|
127 | // fires before user-set events to prevent state order
|
---|
128 | // violations (such as a disconnection before the connection
|
---|
129 | // logic is complete)
|
---|
130 | socket._onconnect();
|
---|
131 | if (fn)
|
---|
132 | fn();
|
---|
133 | // fire user-set events
|
---|
134 | super.emit("connect", socket);
|
---|
135 | super.emit("connection", socket);
|
---|
136 | }
|
---|
137 | else {
|
---|
138 | debug("next called after client was closed - ignoring socket");
|
---|
139 | }
|
---|
140 | });
|
---|
141 | });
|
---|
142 | return socket;
|
---|
143 | }
|
---|
144 | /**
|
---|
145 | * Removes a client. Called by each `Socket`.
|
---|
146 | *
|
---|
147 | * @private
|
---|
148 | */
|
---|
149 | _remove(socket) {
|
---|
150 | if (this.sockets.has(socket.id)) {
|
---|
151 | this.sockets.delete(socket.id);
|
---|
152 | }
|
---|
153 | else {
|
---|
154 | debug("ignoring remove for %s", socket.id);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | /**
|
---|
158 | * Emits to all clients.
|
---|
159 | *
|
---|
160 | * @return Always true
|
---|
161 | * @public
|
---|
162 | */
|
---|
163 | emit(ev, ...args) {
|
---|
164 | if (socket_1.RESERVED_EVENTS.has(ev)) {
|
---|
165 | throw new Error(`"${ev}" is a reserved event name`);
|
---|
166 | }
|
---|
167 | // set up packet object
|
---|
168 | args.unshift(ev);
|
---|
169 | const packet = {
|
---|
170 | type: socket_io_parser_1.PacketType.EVENT,
|
---|
171 | data: args,
|
---|
172 | };
|
---|
173 | if ("function" == typeof args[args.length - 1]) {
|
---|
174 | throw new Error("Callbacks are not supported when broadcasting");
|
---|
175 | }
|
---|
176 | const rooms = new Set(this._rooms);
|
---|
177 | const flags = Object.assign({}, this._flags);
|
---|
178 | // reset flags
|
---|
179 | this._rooms.clear();
|
---|
180 | this._flags = {};
|
---|
181 | this.adapter.broadcast(packet, {
|
---|
182 | rooms: rooms,
|
---|
183 | flags: flags,
|
---|
184 | });
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 | /**
|
---|
188 | * Sends a `message` event to all clients.
|
---|
189 | *
|
---|
190 | * @return self
|
---|
191 | * @public
|
---|
192 | */
|
---|
193 | send(...args) {
|
---|
194 | this.emit("message", ...args);
|
---|
195 | return this;
|
---|
196 | }
|
---|
197 | /**
|
---|
198 | * Sends a `message` event to all clients.
|
---|
199 | *
|
---|
200 | * @return self
|
---|
201 | * @public
|
---|
202 | */
|
---|
203 | write(...args) {
|
---|
204 | this.emit("message", ...args);
|
---|
205 | return this;
|
---|
206 | }
|
---|
207 | /**
|
---|
208 | * Gets a list of clients.
|
---|
209 | *
|
---|
210 | * @return self
|
---|
211 | * @public
|
---|
212 | */
|
---|
213 | allSockets() {
|
---|
214 | if (!this.adapter) {
|
---|
215 | throw new Error("No adapter for this namespace, are you trying to get the list of clients of a dynamic namespace?");
|
---|
216 | }
|
---|
217 | const rooms = new Set(this._rooms);
|
---|
218 | this._rooms.clear();
|
---|
219 | return this.adapter.sockets(rooms);
|
---|
220 | }
|
---|
221 | /**
|
---|
222 | * Sets the compress flag.
|
---|
223 | *
|
---|
224 | * @param compress - if `true`, compresses the sending data
|
---|
225 | * @return self
|
---|
226 | * @public
|
---|
227 | */
|
---|
228 | compress(compress) {
|
---|
229 | this._flags.compress = compress;
|
---|
230 | return this;
|
---|
231 | }
|
---|
232 | /**
|
---|
233 | * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
---|
234 | * receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
---|
235 | * and is in the middle of a request-response cycle).
|
---|
236 | *
|
---|
237 | * @return self
|
---|
238 | * @public
|
---|
239 | */
|
---|
240 | get volatile() {
|
---|
241 | this._flags.volatile = true;
|
---|
242 | return this;
|
---|
243 | }
|
---|
244 | /**
|
---|
245 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
---|
246 | *
|
---|
247 | * @return self
|
---|
248 | * @public
|
---|
249 | */
|
---|
250 | get local() {
|
---|
251 | this._flags.local = true;
|
---|
252 | return this;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | exports.Namespace = Namespace;
|
---|