123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- import { Socket } from "./socket";
- import type { Server } from "./index";
- import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
- import type { Client } from "./client";
- import type { Adapter, Room, SocketId } from "socket.io-adapter";
- import { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
- export interface ExtendedError extends Error {
- data?: any;
- }
- export interface NamespaceReservedEventsMap<ListenEvents extends EventsMap, EmitEvents extends EventsMap, ServerSideEvents extends EventsMap, SocketData> {
- connect: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
- connection: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
- }
- export interface ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData> extends NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
- new_namespace: (namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
- }
- export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;
- export declare class Namespace<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, SocketData = any> extends StrictEventEmitter<ServerSideEvents, EmitEvents, NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData>> {
- readonly name: string;
- readonly sockets: Map<SocketId, Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>;
- adapter: Adapter;
- /** @private */
- readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
- /** @private */
- _fns: Array<(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void>;
- /** @private */
- _ids: number;
- /**
- * Namespace constructor.
- *
- * @param server instance
- * @param name
- */
- constructor(server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, name: string);
- /**
- * Initializes the `Adapter` for this nsp.
- * Run upon changing adapter by `Server#adapter`
- * in addition to the constructor.
- *
- * @private
- */
- _initAdapter(): void;
- /**
- * Sets up namespace middleware.
- *
- * @return self
- * @public
- */
- use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void): this;
- /**
- * Executes the middleware for an incoming client.
- *
- * @param socket - the socket that will get added
- * @param fn - last fn call in the middleware
- * @private
- */
- private run;
- /**
- * Targets a room when emitting.
- *
- * @param room
- * @return self
- * @public
- */
- to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
- /**
- * Targets a room when emitting.
- *
- * @param room
- * @return self
- * @public
- */
- in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
- /**
- * Excludes a room when emitting.
- *
- * @param room
- * @return self
- * @public
- */
- except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
- /**
- * Adds a new client.
- *
- * @return {Socket}
- * @private
- */
- _add(client: Client<ListenEvents, EmitEvents, ServerSideEvents>, query: any, fn?: () => void): Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
- /**
- * Removes a client. Called by each `Socket`.
- *
- * @private
- */
- _remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>): void;
- /**
- * Emits to all clients.
- *
- * @return Always true
- * @public
- */
- emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
- /**
- * Sends a `message` event to all clients.
- *
- * @return self
- * @public
- */
- send(...args: EventParams<EmitEvents, "message">): this;
- /**
- * Sends a `message` event to all clients.
- *
- * @return self
- * @public
- */
- write(...args: EventParams<EmitEvents, "message">): this;
- /**
- * Emit a packet to other Socket.IO servers
- *
- * @param ev - the event name
- * @param args - an array of arguments, which may include an acknowledgement callback at the end
- * @public
- */
- serverSideEmit<Ev extends EventNames<ServerSideEvents>>(ev: Ev, ...args: EventParams<ServerSideEvents, Ev>): boolean;
- /**
- * Called when a packet is received from another Socket.IO server
- *
- * @param args - an array of arguments, which may include an acknowledgement callback at the end
- *
- * @private
- */
- _onServerSideEmit(args: [string, ...any[]]): void;
- /**
- * Gets a list of clients.
- *
- * @return self
- * @public
- */
- allSockets(): Promise<Set<SocketId>>;
- /**
- * Sets the compress flag.
- *
- * @param compress - if `true`, compresses the sending data
- * @return self
- * @public
- */
- compress(compress: boolean): BroadcastOperator<EmitEvents, SocketData>;
- /**
- * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
- * receive messages (because of network slowness or other issues, or because they’re connected through long polling
- * and is in the middle of a request-response cycle).
- *
- * @return self
- * @public
- */
- get volatile(): BroadcastOperator<EmitEvents, SocketData>;
- /**
- * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
- *
- * @return self
- * @public
- */
- get local(): BroadcastOperator<EmitEvents, SocketData>;
- /**
- * Adds a timeout in milliseconds for the next operation
- *
- * <pre><code>
- *
- * io.timeout(1000).emit("some-event", (err, responses) => {
- * // ...
- * });
- *
- * </pre></code>
- *
- * @param timeout
- */
- timeout(timeout: number): BroadcastOperator<EventsMap, unknown>;
- /**
- * Returns the matching socket instances
- *
- * @public
- */
- fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
- /**
- * Makes the matching socket instances join the specified rooms
- *
- * @param room
- * @public
- */
- socketsJoin(room: Room | Room[]): void;
- /**
- * Makes the matching socket instances leave the specified rooms
- *
- * @param room
- * @public
- */
- socketsLeave(room: Room | Room[]): void;
- /**
- * Makes the matching socket instances disconnect
- *
- * @param close - whether to close the underlying connection
- * @public
- */
- disconnectSockets(close?: boolean): void;
- }
|