123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Adapter = void 0;
- const events_1 = require("events");
- class Adapter extends events_1.EventEmitter {
-
-
- constructor(nsp) {
- super();
- this.nsp = nsp;
- this.rooms = new Map();
- this.sids = new Map();
- this.encoder = nsp.server.encoder;
- }
-
-
- init() { }
-
-
- close() { }
-
-
- serverCount() {
- return Promise.resolve(1);
- }
-
-
- addAll(id, rooms) {
- if (!this.sids.has(id)) {
- this.sids.set(id, new Set());
- }
- for (const room of rooms) {
- this.sids.get(id).add(room);
- if (!this.rooms.has(room)) {
- this.rooms.set(room, new Set());
- this.emit("create-room", room);
- }
- if (!this.rooms.get(room).has(id)) {
- this.rooms.get(room).add(id);
- this.emit("join-room", room, id);
- }
- }
- }
-
-
- del(id, room) {
- if (this.sids.has(id)) {
- this.sids.get(id).delete(room);
- }
- this._del(room, id);
- }
- _del(room, id) {
- const _room = this.rooms.get(room);
- if (_room != null) {
- const deleted = _room.delete(id);
- if (deleted) {
- this.emit("leave-room", room, id);
- }
- if (_room.size === 0 && this.rooms.delete(room)) {
- this.emit("delete-room", room);
- }
- }
- }
-
-
- delAll(id) {
- if (!this.sids.has(id)) {
- return;
- }
- for (const room of this.sids.get(id)) {
- this._del(room, id);
- }
- this.sids.delete(id);
- }
-
-
- broadcast(packet, opts) {
- const flags = opts.flags || {};
- const packetOpts = {
- preEncoded: true,
- volatile: flags.volatile,
- compress: flags.compress
- };
- packet.nsp = this.nsp.name;
- const encodedPackets = this.encoder.encode(packet);
- this.apply(opts, socket => {
- if (typeof socket.notifyOutgoingListeners === "function") {
- socket.notifyOutgoingListeners(packet);
- }
- socket.client.writeToEngine(encodedPackets, packetOpts);
- });
- }
-
-
- broadcastWithAck(packet, opts, clientCountCallback, ack) {
- const flags = opts.flags || {};
- const packetOpts = {
- preEncoded: true,
- volatile: flags.volatile,
- compress: flags.compress
- };
- packet.nsp = this.nsp.name;
-
- packet.id = this.nsp._ids++;
- const encodedPackets = this.encoder.encode(packet);
- let clientCount = 0;
- this.apply(opts, socket => {
-
- clientCount++;
-
- socket.acks.set(packet.id, ack);
- if (typeof socket.notifyOutgoingListeners === "function") {
- socket.notifyOutgoingListeners(packet);
- }
- socket.client.writeToEngine(encodedPackets, packetOpts);
- });
- clientCountCallback(clientCount);
- }
-
-
- sockets(rooms) {
- const sids = new Set();
- this.apply({ rooms }, socket => {
- sids.add(socket.id);
- });
- return Promise.resolve(sids);
- }
-
-
- socketRooms(id) {
- return this.sids.get(id);
- }
-
-
- fetchSockets(opts) {
- const sockets = [];
- this.apply(opts, socket => {
- sockets.push(socket);
- });
- return Promise.resolve(sockets);
- }
-
-
- addSockets(opts, rooms) {
- this.apply(opts, socket => {
- socket.join(rooms);
- });
- }
-
-
- delSockets(opts, rooms) {
- this.apply(opts, socket => {
- rooms.forEach(room => socket.leave(room));
- });
- }
-
-
- disconnectSockets(opts, close) {
- this.apply(opts, socket => {
- socket.disconnect(close);
- });
- }
- apply(opts, callback) {
- const rooms = opts.rooms;
- const except = this.computeExceptSids(opts.except);
- if (rooms.size) {
- const ids = new Set();
- for (const room of rooms) {
- if (!this.rooms.has(room))
- continue;
- for (const id of this.rooms.get(room)) {
- if (ids.has(id) || except.has(id))
- continue;
- const socket = this.nsp.sockets.get(id);
- if (socket) {
- callback(socket);
- ids.add(id);
- }
- }
- }
- }
- else {
- for (const [id] of this.sids) {
- if (except.has(id))
- continue;
- const socket = this.nsp.sockets.get(id);
- if (socket)
- callback(socket);
- }
- }
- }
- computeExceptSids(exceptRooms) {
- const exceptSids = new Set();
- if (exceptRooms && exceptRooms.size > 0) {
- for (const room of exceptRooms) {
- if (this.rooms.has(room)) {
- this.rooms.get(room).forEach(sid => exceptSids.add(sid));
- }
- }
- }
- return exceptSids;
- }
-
-
- serverSideEmit(packet) {
- console.warn("this adapter does not support the serverSideEmit() functionality");
- }
- }
- exports.Adapter = Adapter;
|