1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.Adapter = void 0;
|
---|
4 | const events_1 = require("events");
|
---|
5 | class Adapter extends events_1.EventEmitter {
|
---|
6 | /**
|
---|
7 | * In-memory adapter constructor.
|
---|
8 | *
|
---|
9 | * @param {Namespace} nsp
|
---|
10 | */
|
---|
11 | constructor(nsp) {
|
---|
12 | super();
|
---|
13 | this.nsp = nsp;
|
---|
14 | this.rooms = new Map();
|
---|
15 | this.sids = new Map();
|
---|
16 | this.encoder = nsp.server.encoder;
|
---|
17 | }
|
---|
18 | /**
|
---|
19 | * To be overridden
|
---|
20 | */
|
---|
21 | init() { }
|
---|
22 | /**
|
---|
23 | * To be overridden
|
---|
24 | */
|
---|
25 | close() { }
|
---|
26 | /**
|
---|
27 | * Adds a socket to a list of room.
|
---|
28 | *
|
---|
29 | * @param {SocketId} id the socket id
|
---|
30 | * @param {Set<Room>} rooms a set of rooms
|
---|
31 | * @public
|
---|
32 | */
|
---|
33 | addAll(id, rooms) {
|
---|
34 | if (!this.sids.has(id)) {
|
---|
35 | this.sids.set(id, new Set());
|
---|
36 | }
|
---|
37 | for (const room of rooms) {
|
---|
38 | this.sids.get(id).add(room);
|
---|
39 | if (!this.rooms.has(room)) {
|
---|
40 | this.rooms.set(room, new Set());
|
---|
41 | this.emit("create-room", room);
|
---|
42 | }
|
---|
43 | if (!this.rooms.get(room).has(id)) {
|
---|
44 | this.rooms.get(room).add(id);
|
---|
45 | this.emit("join-room", room, id);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | /**
|
---|
50 | * Removes a socket from a room.
|
---|
51 | *
|
---|
52 | * @param {SocketId} id the socket id
|
---|
53 | * @param {Room} room the room name
|
---|
54 | */
|
---|
55 | del(id, room) {
|
---|
56 | if (this.sids.has(id)) {
|
---|
57 | this.sids.get(id).delete(room);
|
---|
58 | }
|
---|
59 | this._del(room, id);
|
---|
60 | }
|
---|
61 | _del(room, id) {
|
---|
62 | if (this.rooms.has(room)) {
|
---|
63 | const deleted = this.rooms.get(room).delete(id);
|
---|
64 | if (deleted) {
|
---|
65 | this.emit("leave-room", room, id);
|
---|
66 | }
|
---|
67 | if (this.rooms.get(room).size === 0) {
|
---|
68 | this.rooms.delete(room);
|
---|
69 | this.emit("delete-room", room);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | /**
|
---|
74 | * Removes a socket from all rooms it's joined.
|
---|
75 | *
|
---|
76 | * @param {SocketId} id the socket id
|
---|
77 | */
|
---|
78 | delAll(id) {
|
---|
79 | if (!this.sids.has(id)) {
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | for (const room of this.sids.get(id)) {
|
---|
83 | this._del(room, id);
|
---|
84 | }
|
---|
85 | this.sids.delete(id);
|
---|
86 | }
|
---|
87 | /**
|
---|
88 | * Broadcasts a packet.
|
---|
89 | *
|
---|
90 | * Options:
|
---|
91 | * - `flags` {Object} flags for this packet
|
---|
92 | * - `except` {Array} sids that should be excluded
|
---|
93 | * - `rooms` {Array} list of rooms to broadcast to
|
---|
94 | *
|
---|
95 | * @param {Object} packet the packet object
|
---|
96 | * @param {Object} opts the options
|
---|
97 | * @public
|
---|
98 | */
|
---|
99 | broadcast(packet, opts) {
|
---|
100 | const rooms = opts.rooms;
|
---|
101 | const except = opts.except || new Set();
|
---|
102 | const flags = opts.flags || {};
|
---|
103 | const packetOpts = {
|
---|
104 | preEncoded: true,
|
---|
105 | volatile: flags.volatile,
|
---|
106 | compress: flags.compress
|
---|
107 | };
|
---|
108 | const ids = new Set();
|
---|
109 | packet.nsp = this.nsp.name;
|
---|
110 | const encodedPackets = this.encoder.encode(packet);
|
---|
111 | if (rooms.size) {
|
---|
112 | for (const room of rooms) {
|
---|
113 | if (!this.rooms.has(room))
|
---|
114 | continue;
|
---|
115 | for (const id of this.rooms.get(room)) {
|
---|
116 | if (ids.has(id) || except.has(id))
|
---|
117 | continue;
|
---|
118 | const socket = this.nsp.sockets.get(id);
|
---|
119 | if (socket) {
|
---|
120 | socket.packet(encodedPackets, packetOpts);
|
---|
121 | ids.add(id);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | else {
|
---|
127 | for (const [id] of this.sids) {
|
---|
128 | if (except.has(id))
|
---|
129 | continue;
|
---|
130 | const socket = this.nsp.sockets.get(id);
|
---|
131 | if (socket)
|
---|
132 | socket.packet(encodedPackets, packetOpts);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | /**
|
---|
137 | * Gets a list of sockets by sid.
|
---|
138 | *
|
---|
139 | * @param {Set<Room>} rooms the explicit set of rooms to check.
|
---|
140 | */
|
---|
141 | sockets(rooms) {
|
---|
142 | const sids = new Set();
|
---|
143 | if (rooms.size) {
|
---|
144 | for (const room of rooms) {
|
---|
145 | if (!this.rooms.has(room))
|
---|
146 | continue;
|
---|
147 | for (const id of this.rooms.get(room)) {
|
---|
148 | if (this.nsp.sockets.has(id)) {
|
---|
149 | sids.add(id);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | else {
|
---|
155 | for (const [id] of this.sids) {
|
---|
156 | if (this.nsp.sockets.has(id))
|
---|
157 | sids.add(id);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | return Promise.resolve(sids);
|
---|
161 | }
|
---|
162 | /**
|
---|
163 | * Gets the list of rooms a given socket has joined.
|
---|
164 | *
|
---|
165 | * @param {SocketId} id the socket id
|
---|
166 | */
|
---|
167 | socketRooms(id) {
|
---|
168 | return this.sids.get(id);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | exports.Adapter = Adapter;
|
---|