[6a3a178] | 1 | import { Socket } from "./socket";
|
---|
| 2 | import type { Server } from "./index";
|
---|
[e29cc2e] | 3 | import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
|
---|
[6a3a178] | 4 | import type { Client } from "./client";
|
---|
| 5 | import type { Adapter, Room, SocketId } from "socket.io-adapter";
|
---|
[e29cc2e] | 6 | import { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
|
---|
[6a3a178] | 7 | export interface ExtendedError extends Error {
|
---|
| 8 | data?: any;
|
---|
| 9 | }
|
---|
[e29cc2e] | 10 | export interface NamespaceReservedEventsMap<ListenEvents extends EventsMap, EmitEvents extends EventsMap, ServerSideEvents extends EventsMap, SocketData> {
|
---|
| 11 | connect: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
|
---|
| 12 | connection: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
|
---|
| 13 | }
|
---|
| 14 | export interface ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData> extends NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
|
---|
| 15 | new_namespace: (namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
|
---|
| 16 | }
|
---|
| 17 | export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;
|
---|
| 18 | 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>> {
|
---|
[6a3a178] | 19 | readonly name: string;
|
---|
[e29cc2e] | 20 | readonly sockets: Map<SocketId, Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>;
|
---|
[6a3a178] | 21 | adapter: Adapter;
|
---|
| 22 | /** @private */
|
---|
[e29cc2e] | 23 | readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
---|
[6a3a178] | 24 | /** @private */
|
---|
[e29cc2e] | 25 | _fns: Array<(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void>;
|
---|
[6a3a178] | 26 | /** @private */
|
---|
| 27 | _ids: number;
|
---|
| 28 | /**
|
---|
| 29 | * Namespace constructor.
|
---|
| 30 | *
|
---|
| 31 | * @param server instance
|
---|
| 32 | * @param name
|
---|
| 33 | */
|
---|
[e29cc2e] | 34 | constructor(server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, name: string);
|
---|
[6a3a178] | 35 | /**
|
---|
| 36 | * Initializes the `Adapter` for this nsp.
|
---|
| 37 | * Run upon changing adapter by `Server#adapter`
|
---|
| 38 | * in addition to the constructor.
|
---|
| 39 | *
|
---|
| 40 | * @private
|
---|
| 41 | */
|
---|
| 42 | _initAdapter(): void;
|
---|
| 43 | /**
|
---|
| 44 | * Sets up namespace middleware.
|
---|
| 45 | *
|
---|
| 46 | * @return self
|
---|
| 47 | * @public
|
---|
| 48 | */
|
---|
[e29cc2e] | 49 | use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void): this;
|
---|
[6a3a178] | 50 | /**
|
---|
| 51 | * Executes the middleware for an incoming client.
|
---|
| 52 | *
|
---|
| 53 | * @param socket - the socket that will get added
|
---|
| 54 | * @param fn - last fn call in the middleware
|
---|
| 55 | * @private
|
---|
| 56 | */
|
---|
| 57 | private run;
|
---|
| 58 | /**
|
---|
| 59 | * Targets a room when emitting.
|
---|
| 60 | *
|
---|
[e29cc2e] | 61 | * @param room
|
---|
[6a3a178] | 62 | * @return self
|
---|
| 63 | * @public
|
---|
| 64 | */
|
---|
[e29cc2e] | 65 | to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 66 | /**
|
---|
| 67 | * Targets a room when emitting.
|
---|
| 68 | *
|
---|
[e29cc2e] | 69 | * @param room
|
---|
| 70 | * @return self
|
---|
| 71 | * @public
|
---|
| 72 | */
|
---|
| 73 | in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
| 74 | /**
|
---|
| 75 | * Excludes a room when emitting.
|
---|
| 76 | *
|
---|
| 77 | * @param room
|
---|
[6a3a178] | 78 | * @return self
|
---|
| 79 | * @public
|
---|
| 80 | */
|
---|
[e29cc2e] | 81 | except(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 82 | /**
|
---|
| 83 | * Adds a new client.
|
---|
| 84 | *
|
---|
| 85 | * @return {Socket}
|
---|
| 86 | * @private
|
---|
| 87 | */
|
---|
[e29cc2e] | 88 | _add(client: Client<ListenEvents, EmitEvents, ServerSideEvents>, query: any, fn?: () => void): Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
---|
[6a3a178] | 89 | /**
|
---|
| 90 | * Removes a client. Called by each `Socket`.
|
---|
| 91 | *
|
---|
| 92 | * @private
|
---|
| 93 | */
|
---|
[e29cc2e] | 94 | _remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>): void;
|
---|
[6a3a178] | 95 | /**
|
---|
| 96 | * Emits to all clients.
|
---|
| 97 | *
|
---|
| 98 | * @return Always true
|
---|
| 99 | * @public
|
---|
| 100 | */
|
---|
[e29cc2e] | 101 | emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
|
---|
[6a3a178] | 102 | /**
|
---|
| 103 | * Sends a `message` event to all clients.
|
---|
| 104 | *
|
---|
| 105 | * @return self
|
---|
| 106 | * @public
|
---|
| 107 | */
|
---|
[e29cc2e] | 108 | send(...args: EventParams<EmitEvents, "message">): this;
|
---|
[6a3a178] | 109 | /**
|
---|
| 110 | * Sends a `message` event to all clients.
|
---|
| 111 | *
|
---|
| 112 | * @return self
|
---|
| 113 | * @public
|
---|
| 114 | */
|
---|
[e29cc2e] | 115 | write(...args: EventParams<EmitEvents, "message">): this;
|
---|
| 116 | /**
|
---|
| 117 | * Emit a packet to other Socket.IO servers
|
---|
| 118 | *
|
---|
| 119 | * @param ev - the event name
|
---|
| 120 | * @param args - an array of arguments, which may include an acknowledgement callback at the end
|
---|
| 121 | * @public
|
---|
| 122 | */
|
---|
| 123 | serverSideEmit<Ev extends EventNames<ServerSideEvents>>(ev: Ev, ...args: EventParams<ServerSideEvents, Ev>): boolean;
|
---|
| 124 | /**
|
---|
| 125 | * Called when a packet is received from another Socket.IO server
|
---|
| 126 | *
|
---|
| 127 | * @param args - an array of arguments, which may include an acknowledgement callback at the end
|
---|
| 128 | *
|
---|
| 129 | * @private
|
---|
| 130 | */
|
---|
| 131 | _onServerSideEmit(args: [string, ...any[]]): void;
|
---|
[6a3a178] | 132 | /**
|
---|
| 133 | * Gets a list of clients.
|
---|
| 134 | *
|
---|
| 135 | * @return self
|
---|
| 136 | * @public
|
---|
| 137 | */
|
---|
| 138 | allSockets(): Promise<Set<SocketId>>;
|
---|
| 139 | /**
|
---|
| 140 | * Sets the compress flag.
|
---|
| 141 | *
|
---|
| 142 | * @param compress - if `true`, compresses the sending data
|
---|
| 143 | * @return self
|
---|
| 144 | * @public
|
---|
| 145 | */
|
---|
[e29cc2e] | 146 | compress(compress: boolean): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 147 | /**
|
---|
| 148 | * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
---|
| 149 | * receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
---|
| 150 | * and is in the middle of a request-response cycle).
|
---|
| 151 | *
|
---|
| 152 | * @return self
|
---|
| 153 | * @public
|
---|
| 154 | */
|
---|
[e29cc2e] | 155 | get volatile(): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 156 | /**
|
---|
| 157 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
---|
| 158 | *
|
---|
| 159 | * @return self
|
---|
| 160 | * @public
|
---|
| 161 | */
|
---|
[e29cc2e] | 162 | get local(): BroadcastOperator<EmitEvents>;
|
---|
| 163 | /**
|
---|
| 164 | * Returns the matching socket instances
|
---|
| 165 | *
|
---|
| 166 | * @public
|
---|
| 167 | */
|
---|
| 168 | fetchSockets(): Promise<RemoteSocket<EmitEvents>[]>;
|
---|
| 169 | /**
|
---|
| 170 | * Makes the matching socket instances join the specified rooms
|
---|
| 171 | *
|
---|
| 172 | * @param room
|
---|
| 173 | * @public
|
---|
| 174 | */
|
---|
| 175 | socketsJoin(room: Room | Room[]): void;
|
---|
| 176 | /**
|
---|
| 177 | * Makes the matching socket instances leave the specified rooms
|
---|
| 178 | *
|
---|
| 179 | * @param room
|
---|
| 180 | * @public
|
---|
| 181 | */
|
---|
| 182 | socketsLeave(room: Room | Room[]): void;
|
---|
| 183 | /**
|
---|
| 184 | * Makes the matching socket instances disconnect
|
---|
| 185 | *
|
---|
| 186 | * @param close - whether to close the underlying connection
|
---|
| 187 | * @public
|
---|
| 188 | */
|
---|
| 189 | disconnectSockets(close?: boolean): void;
|
---|
[6a3a178] | 190 | }
|
---|