1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.RemoteSocket = exports.BroadcastOperator = void 0;
|
4 | const socket_1 = require("./socket");
|
5 | const socket_io_parser_1 = require("socket.io-parser");
|
6 | class BroadcastOperator {
|
7 | constructor(adapter, rooms = new Set(), exceptRooms = new Set(), flags = {}) {
|
8 | this.adapter = adapter;
|
9 | this.rooms = rooms;
|
10 | this.exceptRooms = exceptRooms;
|
11 | this.flags = flags;
|
12 | }
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | to(room) {
|
21 | const rooms = new Set(this.rooms);
|
22 | if (Array.isArray(room)) {
|
23 | room.forEach((r) => rooms.add(r));
|
24 | }
|
25 | else {
|
26 | rooms.add(room);
|
27 | }
|
28 | return new BroadcastOperator(this.adapter, rooms, this.exceptRooms, this.flags);
|
29 | }
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | in(room) {
|
38 | return this.to(room);
|
39 | }
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | except(room) {
|
48 | const exceptRooms = new Set(this.exceptRooms);
|
49 | if (Array.isArray(room)) {
|
50 | room.forEach((r) => exceptRooms.add(r));
|
51 | }
|
52 | else {
|
53 | exceptRooms.add(room);
|
54 | }
|
55 | return new BroadcastOperator(this.adapter, this.rooms, exceptRooms, this.flags);
|
56 | }
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | compress(compress) {
|
65 | const flags = Object.assign({}, this.flags, { compress });
|
66 | return new BroadcastOperator(this.adapter, this.rooms, this.exceptRooms, flags);
|
67 | }
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | get volatile() {
|
77 | const flags = Object.assign({}, this.flags, { volatile: true });
|
78 | return new BroadcastOperator(this.adapter, this.rooms, this.exceptRooms, flags);
|
79 | }
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | get local() {
|
87 | const flags = Object.assign({}, this.flags, { local: true });
|
88 | return new BroadcastOperator(this.adapter, this.rooms, this.exceptRooms, flags);
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | emit(ev, ...args) {
|
97 | if (socket_1.RESERVED_EVENTS.has(ev)) {
|
98 | throw new Error(`"${ev}" is a reserved event name`);
|
99 | }
|
100 |
|
101 | const data = [ev, ...args];
|
102 | const packet = {
|
103 | type: socket_io_parser_1.PacketType.EVENT,
|
104 | data: data,
|
105 | };
|
106 | if ("function" == typeof data[data.length - 1]) {
|
107 | throw new Error("Callbacks are not supported when broadcasting");
|
108 | }
|
109 | this.adapter.broadcast(packet, {
|
110 | rooms: this.rooms,
|
111 | except: this.exceptRooms,
|
112 | flags: this.flags,
|
113 | });
|
114 | return true;
|
115 | }
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 | allSockets() {
|
122 | if (!this.adapter) {
|
123 | throw new Error("No adapter for this namespace, are you trying to get the list of clients of a dynamic namespace?");
|
124 | }
|
125 | return this.adapter.sockets(this.rooms);
|
126 | }
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 | fetchSockets() {
|
133 | return this.adapter
|
134 | .fetchSockets({
|
135 | rooms: this.rooms,
|
136 | except: this.exceptRooms,
|
137 | })
|
138 | .then((sockets) => {
|
139 | return sockets.map((socket) => {
|
140 | if (socket instanceof socket_1.Socket) {
|
141 |
|
142 | return socket;
|
143 | }
|
144 | else {
|
145 | return new RemoteSocket(this.adapter, socket);
|
146 | }
|
147 | });
|
148 | });
|
149 | }
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | socketsJoin(room) {
|
157 | this.adapter.addSockets({
|
158 | rooms: this.rooms,
|
159 | except: this.exceptRooms,
|
160 | }, Array.isArray(room) ? room : [room]);
|
161 | }
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | socketsLeave(room) {
|
169 | this.adapter.delSockets({
|
170 | rooms: this.rooms,
|
171 | except: this.exceptRooms,
|
172 | }, Array.isArray(room) ? room : [room]);
|
173 | }
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | disconnectSockets(close = false) {
|
181 | this.adapter.disconnectSockets({
|
182 | rooms: this.rooms,
|
183 | except: this.exceptRooms,
|
184 | }, close);
|
185 | }
|
186 | }
|
187 | exports.BroadcastOperator = BroadcastOperator;
|
188 |
|
189 |
|
190 |
|
191 | class RemoteSocket {
|
192 | constructor(adapter, details) {
|
193 | this.id = details.id;
|
194 | this.handshake = details.handshake;
|
195 | this.rooms = new Set(details.rooms);
|
196 | this.data = details.data;
|
197 | this.operator = new BroadcastOperator(adapter, new Set([this.id]));
|
198 | }
|
199 | emit(ev, ...args) {
|
200 | return this.operator.emit(ev, ...args);
|
201 | }
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | join(room) {
|
209 | return this.operator.socketsJoin(room);
|
210 | }
|
211 | |
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | leave(room) {
|
218 | return this.operator.socketsLeave(room);
|
219 | }
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | disconnect(close = false) {
|
229 | this.operator.disconnectSockets(close);
|
230 | return this;
|
231 | }
|
232 | }
|
233 | exports.RemoteSocket = RemoteSocket;
|