UNPKG

9.6 kBTypeScriptView Raw
1/// <reference types="node" />
2import http = require("http");
3import { ServerOptions as EngineOptions, AttachOptions } from "engine.io";
4import { ExtendedError, Namespace, ServerReservedEventsMap } from "./namespace";
5import { Adapter, Room, SocketId } from "socket.io-adapter";
6import * as parser from "socket.io-parser";
7import type { Encoder } from "socket.io-parser";
8import { Socket } from "./socket";
9import type { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
10import { EventsMap, DefaultEventsMap, EventParams, StrictEventEmitter, EventNames } from "./typed-events";
11declare type ParentNspNameMatchFn = (name: string, auth: {
12 [key: string]: any;
13}, fn: (err: Error | null, success: boolean) => void) => void;
14declare type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter);
15interface ServerOptions extends EngineOptions, AttachOptions {
16 /**
17 * name of the path to capture
18 * @default "/socket.io"
19 */
20 path: string;
21 /**
22 * whether to serve the client files
23 * @default true
24 */
25 serveClient: boolean;
26 /**
27 * the adapter to use
28 * @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
29 */
30 adapter: AdapterConstructor;
31 /**
32 * the parser to use
33 * @default the default parser (https://github.com/socketio/socket.io-parser)
34 */
35 parser: any;
36 /**
37 * how many ms before a client without namespace is closed
38 * @default 45000
39 */
40 connectTimeout: number;
41}
42export declare class Server<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap> extends StrictEventEmitter<ServerSideEvents, EmitEvents, ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>> {
43 readonly sockets: Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
44 /**
45 * A reference to the underlying Engine.IO server.
46 *
47 * Example:
48 *
49 * <code>
50 * const clientsCount = io.engine.clientsCount;
51 * </code>
52 *
53 */
54 engine: any;
55 /** @private */
56 readonly _parser: typeof parser;
57 /** @private */
58 readonly encoder: Encoder;
59 /**
60 * @private
61 */
62 _nsps: Map<string, Namespace<ListenEvents, EmitEvents, ServerSideEvents>>;
63 private parentNsps;
64 private _adapter?;
65 private _serveClient;
66 private opts;
67 private eio;
68 private _path;
69 private clientPathRegex;
70 /**
71 * @private
72 */
73 _connectTimeout: number;
74 private httpServer;
75 /**
76 * Server constructor.
77 *
78 * @param srv http server, port, or options
79 * @param [opts]
80 * @public
81 */
82 constructor(opts?: Partial<ServerOptions>);
83 constructor(srv?: http.Server | number, opts?: Partial<ServerOptions>);
84 constructor(srv: undefined | Partial<ServerOptions> | http.Server | number, opts?: Partial<ServerOptions>);
85 /**
86 * Sets/gets whether client code is being served.
87 *
88 * @param v - whether to serve client code
89 * @return self when setting or value when getting
90 * @public
91 */
92 serveClient(v: boolean): this;
93 serveClient(): boolean;
94 serveClient(v?: boolean): this | boolean;
95 /**
96 * Executes the middleware for an incoming namespace not already created on the server.
97 *
98 * @param name - name of incoming namespace
99 * @param auth - the auth parameters
100 * @param fn - callback
101 *
102 * @private
103 */
104 _checkNamespace(name: string, auth: {
105 [key: string]: any;
106 }, fn: (nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents> | false) => void): void;
107 /**
108 * Sets the client serving path.
109 *
110 * @param {String} v pathname
111 * @return {Server|String} self when setting or value when getting
112 * @public
113 */
114 path(v: string): this;
115 path(): string;
116 path(v?: string): this | string;
117 /**
118 * Set the delay after which a client without namespace is closed
119 * @param v
120 * @public
121 */
122 connectTimeout(v: number): this;
123 connectTimeout(): number;
124 connectTimeout(v?: number): this | number;
125 /**
126 * Sets the adapter for rooms.
127 *
128 * @param v pathname
129 * @return self when setting or value when getting
130 * @public
131 */
132 adapter(): AdapterConstructor | undefined;
133 adapter(v: AdapterConstructor): this;
134 /**
135 * Attaches socket.io to a server or port.
136 *
137 * @param srv - server or port
138 * @param opts - options passed to engine.io
139 * @return self
140 * @public
141 */
142 listen(srv: http.Server | number, opts?: Partial<ServerOptions>): this;
143 /**
144 * Attaches socket.io to a server or port.
145 *
146 * @param srv - server or port
147 * @param opts - options passed to engine.io
148 * @return self
149 * @public
150 */
151 attach(srv: http.Server | number, opts?: Partial<ServerOptions>): this;
152 /**
153 * Initialize engine
154 *
155 * @param srv - the server to attach to
156 * @param opts - options passed to engine.io
157 * @private
158 */
159 private initEngine;
160 /**
161 * Attaches the static file serving.
162 *
163 * @param srv http server
164 * @private
165 */
166 private attachServe;
167 /**
168 * Handles a request serving of client source and map
169 *
170 * @param req
171 * @param res
172 * @private
173 */
174 private serve;
175 /**
176 * @param filename
177 * @param req
178 * @param res
179 * @private
180 */
181 private static sendFile;
182 /**
183 * Binds socket.io to an engine.io instance.
184 *
185 * @param {engine.Server} engine engine.io (or compatible) server
186 * @return self
187 * @public
188 */
189 bind(engine: any): this;
190 /**
191 * Called with each incoming transport connection.
192 *
193 * @param {engine.Socket} conn
194 * @return self
195 * @private
196 */
197 private onconnection;
198 /**
199 * Looks up a namespace.
200 *
201 * @param {String|RegExp|Function} name nsp name
202 * @param fn optional, nsp `connection` ev handler
203 * @public
204 */
205 of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>) => void): Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
206 /**
207 * Closes server connection
208 *
209 * @param [fn] optional, called as `fn([err])` on error OR all conns closed
210 * @public
211 */
212 close(fn?: (err?: Error) => void): void;
213 /**
214 * Sets up namespace middleware.
215 *
216 * @return self
217 * @public
218 */
219 use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>, next: (err?: ExtendedError) => void) => void): this;
220 /**
221 * Targets a room when emitting.
222 *
223 * @param room
224 * @return self
225 * @public
226 */
227 to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
228 /**
229 * Targets a room when emitting.
230 *
231 * @param room
232 * @return self
233 * @public
234 */
235 in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
236 /**
237 * Excludes a room when emitting.
238 *
239 * @param name
240 * @return self
241 * @public
242 */
243 except(name: Room | Room[]): BroadcastOperator<EmitEvents>;
244 /**
245 * Sends a `message` event to all clients.
246 *
247 * @return self
248 * @public
249 */
250 send(...args: EventParams<EmitEvents, "message">): this;
251 /**
252 * Sends a `message` event to all clients.
253 *
254 * @return self
255 * @public
256 */
257 write(...args: EventParams<EmitEvents, "message">): this;
258 /**
259 * Emit a packet to other Socket.IO servers
260 *
261 * @param ev - the event name
262 * @param args - an array of arguments, which may include an acknowledgement callback at the end
263 * @public
264 */
265 serverSideEmit<Ev extends EventNames<ServerSideEvents>>(ev: Ev, ...args: EventParams<ServerSideEvents, Ev>): boolean;
266 /**
267 * Gets a list of socket ids.
268 *
269 * @public
270 */
271 allSockets(): Promise<Set<SocketId>>;
272 /**
273 * Sets the compress flag.
274 *
275 * @param compress - if `true`, compresses the sending data
276 * @return self
277 * @public
278 */
279 compress(compress: boolean): BroadcastOperator<EmitEvents>;
280 /**
281 * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
282 * receive messages (because of network slowness or other issues, or because they’re connected through long polling
283 * and is in the middle of a request-response cycle).
284 *
285 * @return self
286 * @public
287 */
288 get volatile(): BroadcastOperator<EmitEvents>;
289 /**
290 * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
291 *
292 * @return self
293 * @public
294 */
295 get local(): BroadcastOperator<EmitEvents>;
296 /**
297 * Returns the matching socket instances
298 *
299 * @public
300 */
301 fetchSockets(): Promise<RemoteSocket<EmitEvents>[]>;
302 /**
303 * Makes the matching socket instances join the specified rooms
304 *
305 * @param room
306 * @public
307 */
308 socketsJoin(room: Room | Room[]): void;
309 /**
310 * Makes the matching socket instances leave the specified rooms
311 *
312 * @param room
313 * @public
314 */
315 socketsLeave(room: Room | Room[]): void;
316 /**
317 * Makes the matching socket instances disconnect
318 *
319 * @param close - whether to close the underlying connection
320 * @public
321 */
322 disconnectSockets(close?: boolean): void;
323}
324export { Socket, ServerOptions, Namespace, BroadcastOperator, RemoteSocket };
325
\No newline at end of file