index.d.ts
9.5 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/// <reference types="node" />
import http = require("http");
import { ServerOptions as EngineOptions, AttachOptions } from "engine.io";
import { ExtendedError, Namespace, ServerReservedEventsMap } from "./namespace";
import { Adapter, Room, SocketId } from "socket.io-adapter";
import * as parser from "socket.io-parser";
import type { Encoder } from "socket.io-parser";
import { Socket } from "./socket";
import type { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
import { EventsMap, DefaultEventsMap, EventParams, StrictEventEmitter, EventNames } from "./typed-events";
declare type ParentNspNameMatchFn = (name: string, auth: {
[key: string]: any;
}, fn: (err: Error | null, success: boolean) => void) => void;
declare type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter);
interface ServerOptions extends EngineOptions, AttachOptions {
/**
* name of the path to capture
* @default "/socket.io"
*/
path: string;
/**
* whether to serve the client files
* @default true
*/
serveClient: boolean;
/**
* the adapter to use
* @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
*/
adapter: AdapterConstructor;
/**
* the parser to use
* @default the default parser (https://github.com/socketio/socket.io-parser)
*/
parser: any;
/**
* how many ms before a client without namespace is closed
* @default 45000
*/
connectTimeout: number;
}
export declare class Server<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, SocketData = any> extends StrictEventEmitter<ServerSideEvents, EmitEvents, ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData>> {
readonly sockets: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
/**
* A reference to the underlying Engine.IO server.
*
* Example:
*
* <code>
* const clientsCount = io.engine.clientsCount;
* </code>
*
*/
engine: any;
/** @private */
readonly _parser: typeof parser;
/** @private */
readonly encoder: Encoder;
/**
* @private
*/
_nsps: Map<string, Namespace<ListenEvents, EmitEvents, ServerSideEvents>>;
private parentNsps;
private _adapter?;
private _serveClient;
private opts;
private eio;
private _path;
private clientPathRegex;
/**
* @private
*/
_connectTimeout: number;
private httpServer;
/**
* Server constructor.
*
* @param srv http server, port, or options
* @param [opts]
* @public
*/
constructor(opts?: Partial<ServerOptions>);
constructor(srv?: http.Server | number, opts?: Partial<ServerOptions>);
constructor(srv: undefined | Partial<ServerOptions> | http.Server | number, opts?: Partial<ServerOptions>);
/**
* Sets/gets whether client code is being served.
*
* @param v - whether to serve client code
* @return self when setting or value when getting
* @public
*/
serveClient(v: boolean): this;
serveClient(): boolean;
serveClient(v?: boolean): this | boolean;
/**
* Executes the middleware for an incoming namespace not already created on the server.
*
* @param name - name of incoming namespace
* @param auth - the auth parameters
* @param fn - callback
*
* @private
*/
_checkNamespace(name: string, auth: {
[key: string]: any;
}, fn: (nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents> | false) => void): void;
/**
* Sets the client serving path.
*
* @param {String} v pathname
* @return {Server|String} self when setting or value when getting
* @public
*/
path(v: string): this;
path(): string;
path(v?: string): this | string;
/**
* Set the delay after which a client without namespace is closed
* @param v
* @public
*/
connectTimeout(v: number): this;
connectTimeout(): number;
connectTimeout(v?: number): this | number;
/**
* Sets the adapter for rooms.
*
* @param v pathname
* @return self when setting or value when getting
* @public
*/
adapter(): AdapterConstructor | undefined;
adapter(v: AdapterConstructor): this;
/**
* Attaches socket.io to a server or port.
*
* @param srv - server or port
* @param opts - options passed to engine.io
* @return self
* @public
*/
listen(srv: http.Server | number, opts?: Partial<ServerOptions>): this;
/**
* Attaches socket.io to a server or port.
*
* @param srv - server or port
* @param opts - options passed to engine.io
* @return self
* @public
*/
attach(srv: http.Server | number, opts?: Partial<ServerOptions>): this;
attachApp(app: any, opts?: Partial<ServerOptions>): void;
/**
* Initialize engine
*
* @param srv - the server to attach to
* @param opts - options passed to engine.io
* @private
*/
private initEngine;
/**
* Attaches the static file serving.
*
* @param srv http server
* @private
*/
private attachServe;
/**
* Handles a request serving of client source and map
*
* @param req
* @param res
* @private
*/
private serve;
/**
* @param filename
* @param req
* @param res
* @private
*/
private static sendFile;
/**
* Binds socket.io to an engine.io instance.
*
* @param {engine.Server} engine engine.io (or compatible) server
* @return self
* @public
*/
bind(engine: any): this;
/**
* Called with each incoming transport connection.
*
* @param {engine.Socket} conn
* @return self
* @private
*/
private onconnection;
/**
* Looks up a namespace.
*
* @param {String|RegExp|Function} name nsp name
* @param fn optional, nsp `connection` ev handler
* @public
*/
of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void): Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
/**
* Closes server connection
*
* @param [fn] optional, called as `fn([err])` on error OR all conns closed
* @public
*/
close(fn?: (err?: Error) => void): void;
/**
* Sets up namespace middleware.
*
* @return self
* @public
*/
use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void): this;
/**
* 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 name
* @return self
* @public
*/
except(name: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* 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;
/**
* Gets a list of socket ids.
*
* @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;
}
export { Socket, ServerOptions, Namespace, BroadcastOperator, RemoteSocket };