123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.RemoteSocket = exports.BroadcastOperator = void 0;
- const socket_1 = require("./socket");
- const socket_io_parser_1 = require("socket.io-parser");
- class BroadcastOperator {
- constructor(adapter, rooms = new Set(), exceptRooms = new Set(), flags = {}) {
- this.adapter = adapter;
- this.rooms = rooms;
- this.exceptRooms = exceptRooms;
- this.flags = flags;
- }
-
- to(room) {
- const rooms = new Set(this.rooms);
- if (Array.isArray(room)) {
- room.forEach((r) => rooms.add(r));
- }
- else {
- rooms.add(room);
- }
- return new BroadcastOperator(this.adapter, rooms, this.exceptRooms, this.flags);
- }
-
- in(room) {
- return this.to(room);
- }
-
- except(room) {
- const exceptRooms = new Set(this.exceptRooms);
- if (Array.isArray(room)) {
- room.forEach((r) => exceptRooms.add(r));
- }
- else {
- exceptRooms.add(room);
- }
- return new BroadcastOperator(this.adapter, this.rooms, exceptRooms, this.flags);
- }
-
- compress(compress) {
- const flags = Object.assign({}, this.flags, { compress });
- return new BroadcastOperator(this.adapter, this.rooms, this.exceptRooms, flags);
- }
-
- get volatile() {
- const flags = Object.assign({}, this.flags, { volatile: true });
- return new BroadcastOperator(this.adapter, this.rooms, this.exceptRooms, flags);
- }
-
- get local() {
- const flags = Object.assign({}, this.flags, { local: true });
- return new BroadcastOperator(this.adapter, this.rooms, this.exceptRooms, flags);
- }
-
- emit(ev, ...args) {
- if (socket_1.RESERVED_EVENTS.has(ev)) {
- throw new Error(`"${ev}" is a reserved event name`);
- }
-
- const data = [ev, ...args];
- const packet = {
- type: socket_io_parser_1.PacketType.EVENT,
- data: data,
- };
- if ("function" == typeof data[data.length - 1]) {
- throw new Error("Callbacks are not supported when broadcasting");
- }
- this.adapter.broadcast(packet, {
- rooms: this.rooms,
- except: this.exceptRooms,
- flags: this.flags,
- });
- return true;
- }
-
- allSockets() {
- if (!this.adapter) {
- throw new Error("No adapter for this namespace, are you trying to get the list of clients of a dynamic namespace?");
- }
- return this.adapter.sockets(this.rooms);
- }
-
- fetchSockets() {
- return this.adapter
- .fetchSockets({
- rooms: this.rooms,
- except: this.exceptRooms,
- })
- .then((sockets) => {
- return sockets.map((socket) => {
- if (socket instanceof socket_1.Socket) {
-
- return socket;
- }
- else {
- return new RemoteSocket(this.adapter, socket);
- }
- });
- });
- }
-
- socketsJoin(room) {
- this.adapter.addSockets({
- rooms: this.rooms,
- except: this.exceptRooms,
- }, Array.isArray(room) ? room : [room]);
- }
-
- socketsLeave(room) {
- this.adapter.delSockets({
- rooms: this.rooms,
- except: this.exceptRooms,
- }, Array.isArray(room) ? room : [room]);
- }
-
- disconnectSockets(close = false) {
- this.adapter.disconnectSockets({
- rooms: this.rooms,
- except: this.exceptRooms,
- }, close);
- }
- }
- exports.BroadcastOperator = BroadcastOperator;
- class RemoteSocket {
- constructor(adapter, details) {
- this.id = details.id;
- this.handshake = details.handshake;
- this.rooms = new Set(details.rooms);
- this.data = details.data;
- this.operator = new BroadcastOperator(adapter, new Set([this.id]));
- }
- emit(ev, ...args) {
- return this.operator.emit(ev, ...args);
- }
-
- join(room) {
- return this.operator.socketsJoin(room);
- }
-
- leave(room) {
- return this.operator.socketsLeave(room);
- }
-
- disconnect(close = false) {
- this.operator.disconnectSockets(close);
- return this;
- }
- }
- exports.RemoteSocket = RemoteSocket;
|