UNPKG

6.37 kBTypeScriptView Raw
1import { Socket } from "./socket";
2import type { Server } from "./index";
3import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
4import type { Client } from "./client";
5import type { Adapter, Room, SocketId } from "socket.io-adapter";
6import { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
7export interface ExtendedError extends Error {
8 data?: any;
9}
10export interface NamespaceReservedEventsMap<ListenEvents extends EventsMap, EmitEvents extends EventsMap, ServerSideEvents extends EventsMap> {
11 connect: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>) => void;
12 connection: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>) => void;
13}
14export interface ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents> extends NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents> {
15 new_namespace: (namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents>) => void;
16}
17export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;
18export declare class Namespace<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap> extends StrictEventEmitter<ServerSideEvents, EmitEvents, NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>> {
19 readonly name: string;
20 readonly sockets: Map<SocketId, Socket<ListenEvents, EmitEvents, ServerSideEvents>>;
21 adapter: Adapter;
22 /** @private */
23 readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents>;
24 /** @private */
25 _fns: Array<(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>, next: (err?: ExtendedError) => void) => void>;
26 /** @private */
27 _ids: number;
28 /**
29 * Namespace constructor.
30 *
31 * @param server instance
32 * @param name
33 */
34 constructor(server: Server<ListenEvents, EmitEvents, ServerSideEvents>, name: string);
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 */
49 use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>, next: (err?: ExtendedError) => void) => void): this;
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 *
61 * @param room
62 * @return self
63 * @public
64 */
65 to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
66 /**
67 * Targets a room when emitting.
68 *
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
78 * @return self
79 * @public
80 */
81 except(room: Room | Room[]): BroadcastOperator<EmitEvents>;
82 /**
83 * Adds a new client.
84 *
85 * @return {Socket}
86 * @private
87 */
88 _add(client: Client<ListenEvents, EmitEvents, ServerSideEvents>, query: any, fn?: () => void): Socket<ListenEvents, EmitEvents, ServerSideEvents>;
89 /**
90 * Removes a client. Called by each `Socket`.
91 *
92 * @private
93 */
94 _remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>): void;
95 /**
96 * Emits to all clients.
97 *
98 * @return Always true
99 * @public
100 */
101 emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
102 /**
103 * Sends a `message` event to all clients.
104 *
105 * @return self
106 * @public
107 */
108 send(...args: EventParams<EmitEvents, "message">): this;
109 /**
110 * Sends a `message` event to all clients.
111 *
112 * @return self
113 * @public
114 */
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;
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 */
146 compress(compress: boolean): BroadcastOperator<EmitEvents>;
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 */
155 get volatile(): BroadcastOperator<EmitEvents>;
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 */
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;
190}