namespace.d.ts
6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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>;
/**
* Targets a room when emitting.
*
* @param room
* @return self
* @public
*/
in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Excludes a room when emitting.
*
* @param room
* @return self
* @public
*/
except(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* 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>;
/**
* 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>;
/**
* 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>;
/**
* Returns the matching socket instances
*
* @public
*/
fetchSockets(): Promise<RemoteSocket<EmitEvents>[]>;
/**
* 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;
}