[6a3a178] | 1 | /// <reference types="node" />
|
---|
| 2 | import http = require("http");
|
---|
[e29cc2e] | 3 | import { ServerOptions as EngineOptions, AttachOptions } from "engine.io";
|
---|
| 4 | import { ExtendedError, Namespace, ServerReservedEventsMap } from "./namespace";
|
---|
[6a3a178] | 5 | import { Adapter, Room, SocketId } from "socket.io-adapter";
|
---|
| 6 | import * as parser from "socket.io-parser";
|
---|
| 7 | import type { Encoder } from "socket.io-parser";
|
---|
| 8 | import { Socket } from "./socket";
|
---|
[e29cc2e] | 9 | import type { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
|
---|
| 10 | import { EventsMap, DefaultEventsMap, EventParams, StrictEventEmitter, EventNames } from "./typed-events";
|
---|
[6a3a178] | 11 | declare type ParentNspNameMatchFn = (name: string, auth: {
|
---|
| 12 | [key: string]: any;
|
---|
| 13 | }, fn: (err: Error | null, success: boolean) => void) => void;
|
---|
[e29cc2e] | 14 | declare type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter);
|
---|
| 15 | interface ServerOptions extends EngineOptions, AttachOptions {
|
---|
[6a3a178] | 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 | */
|
---|
[e29cc2e] | 30 | adapter: AdapterConstructor;
|
---|
[6a3a178] | 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 | }
|
---|
[e29cc2e] | 42 | 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>> {
|
---|
| 43 | readonly sockets: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
---|
| 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;
|
---|
[6a3a178] | 55 | /** @private */
|
---|
| 56 | readonly _parser: typeof parser;
|
---|
| 57 | /** @private */
|
---|
| 58 | readonly encoder: Encoder;
|
---|
| 59 | /**
|
---|
| 60 | * @private
|
---|
| 61 | */
|
---|
[e29cc2e] | 62 | _nsps: Map<string, Namespace<ListenEvents, EmitEvents, ServerSideEvents>>;
|
---|
[6a3a178] | 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;
|
---|
[e29cc2e] | 106 | }, fn: (nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents> | false) => void): void;
|
---|
[6a3a178] | 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 | */
|
---|
[e29cc2e] | 132 | adapter(): AdapterConstructor | undefined;
|
---|
| 133 | adapter(v: AdapterConstructor): this;
|
---|
[6a3a178] | 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;
|
---|
[e29cc2e] | 152 | attachApp(app: any, opts?: Partial<ServerOptions>): void;
|
---|
[6a3a178] | 153 | /**
|
---|
| 154 | * Initialize engine
|
---|
| 155 | *
|
---|
| 156 | * @param srv - the server to attach to
|
---|
| 157 | * @param opts - options passed to engine.io
|
---|
| 158 | * @private
|
---|
| 159 | */
|
---|
| 160 | private initEngine;
|
---|
| 161 | /**
|
---|
| 162 | * Attaches the static file serving.
|
---|
| 163 | *
|
---|
| 164 | * @param srv http server
|
---|
| 165 | * @private
|
---|
| 166 | */
|
---|
| 167 | private attachServe;
|
---|
| 168 | /**
|
---|
| 169 | * Handles a request serving of client source and map
|
---|
| 170 | *
|
---|
| 171 | * @param req
|
---|
| 172 | * @param res
|
---|
| 173 | * @private
|
---|
| 174 | */
|
---|
| 175 | private serve;
|
---|
| 176 | /**
|
---|
| 177 | * @param filename
|
---|
| 178 | * @param req
|
---|
| 179 | * @param res
|
---|
| 180 | * @private
|
---|
| 181 | */
|
---|
| 182 | private static sendFile;
|
---|
| 183 | /**
|
---|
| 184 | * Binds socket.io to an engine.io instance.
|
---|
| 185 | *
|
---|
| 186 | * @param {engine.Server} engine engine.io (or compatible) server
|
---|
| 187 | * @return self
|
---|
| 188 | * @public
|
---|
| 189 | */
|
---|
| 190 | bind(engine: any): this;
|
---|
| 191 | /**
|
---|
| 192 | * Called with each incoming transport connection.
|
---|
| 193 | *
|
---|
| 194 | * @param {engine.Socket} conn
|
---|
| 195 | * @return self
|
---|
| 196 | * @private
|
---|
| 197 | */
|
---|
| 198 | private onconnection;
|
---|
| 199 | /**
|
---|
| 200 | * Looks up a namespace.
|
---|
| 201 | *
|
---|
| 202 | * @param {String|RegExp|Function} name nsp name
|
---|
| 203 | * @param fn optional, nsp `connection` ev handler
|
---|
| 204 | * @public
|
---|
| 205 | */
|
---|
[e29cc2e] | 206 | of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void): Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
|
---|
[6a3a178] | 207 | /**
|
---|
| 208 | * Closes server connection
|
---|
| 209 | *
|
---|
| 210 | * @param [fn] optional, called as `fn([err])` on error OR all conns closed
|
---|
| 211 | * @public
|
---|
| 212 | */
|
---|
| 213 | close(fn?: (err?: Error) => void): void;
|
---|
| 214 | /**
|
---|
| 215 | * Sets up namespace middleware.
|
---|
| 216 | *
|
---|
| 217 | * @return self
|
---|
| 218 | * @public
|
---|
| 219 | */
|
---|
[e29cc2e] | 220 | use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void): this;
|
---|
[6a3a178] | 221 | /**
|
---|
| 222 | * Targets a room when emitting.
|
---|
| 223 | *
|
---|
[e29cc2e] | 224 | * @param room
|
---|
[6a3a178] | 225 | * @return self
|
---|
| 226 | * @public
|
---|
| 227 | */
|
---|
[e29cc2e] | 228 | to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 229 | /**
|
---|
| 230 | * Targets a room when emitting.
|
---|
| 231 | *
|
---|
[e29cc2e] | 232 | * @param room
|
---|
| 233 | * @return self
|
---|
| 234 | * @public
|
---|
| 235 | */
|
---|
| 236 | in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
| 237 | /**
|
---|
| 238 | * Excludes a room when emitting.
|
---|
| 239 | *
|
---|
[6a3a178] | 240 | * @param name
|
---|
| 241 | * @return self
|
---|
| 242 | * @public
|
---|
| 243 | */
|
---|
[e29cc2e] | 244 | except(name: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 245 | /**
|
---|
| 246 | * Sends a `message` event to all clients.
|
---|
| 247 | *
|
---|
| 248 | * @return self
|
---|
| 249 | * @public
|
---|
| 250 | */
|
---|
[e29cc2e] | 251 | send(...args: EventParams<EmitEvents, "message">): this;
|
---|
[6a3a178] | 252 | /**
|
---|
| 253 | * Sends a `message` event to all clients.
|
---|
| 254 | *
|
---|
| 255 | * @return self
|
---|
| 256 | * @public
|
---|
| 257 | */
|
---|
[e29cc2e] | 258 | write(...args: EventParams<EmitEvents, "message">): this;
|
---|
| 259 | /**
|
---|
| 260 | * Emit a packet to other Socket.IO servers
|
---|
| 261 | *
|
---|
| 262 | * @param ev - the event name
|
---|
| 263 | * @param args - an array of arguments, which may include an acknowledgement callback at the end
|
---|
| 264 | * @public
|
---|
| 265 | */
|
---|
| 266 | serverSideEmit<Ev extends EventNames<ServerSideEvents>>(ev: Ev, ...args: EventParams<ServerSideEvents, Ev>): boolean;
|
---|
[6a3a178] | 267 | /**
|
---|
| 268 | * Gets a list of socket ids.
|
---|
| 269 | *
|
---|
| 270 | * @public
|
---|
| 271 | */
|
---|
| 272 | allSockets(): Promise<Set<SocketId>>;
|
---|
| 273 | /**
|
---|
| 274 | * Sets the compress flag.
|
---|
| 275 | *
|
---|
| 276 | * @param compress - if `true`, compresses the sending data
|
---|
| 277 | * @return self
|
---|
| 278 | * @public
|
---|
| 279 | */
|
---|
[e29cc2e] | 280 | compress(compress: boolean): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 281 | /**
|
---|
| 282 | * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
---|
| 283 | * receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
---|
| 284 | * and is in the middle of a request-response cycle).
|
---|
| 285 | *
|
---|
| 286 | * @return self
|
---|
| 287 | * @public
|
---|
| 288 | */
|
---|
[e29cc2e] | 289 | get volatile(): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 290 | /**
|
---|
| 291 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
---|
| 292 | *
|
---|
| 293 | * @return self
|
---|
| 294 | * @public
|
---|
| 295 | */
|
---|
[e29cc2e] | 296 | get local(): BroadcastOperator<EmitEvents>;
|
---|
| 297 | /**
|
---|
| 298 | * Returns the matching socket instances
|
---|
| 299 | *
|
---|
| 300 | * @public
|
---|
| 301 | */
|
---|
| 302 | fetchSockets(): Promise<RemoteSocket<EmitEvents>[]>;
|
---|
| 303 | /**
|
---|
| 304 | * Makes the matching socket instances join the specified rooms
|
---|
| 305 | *
|
---|
| 306 | * @param room
|
---|
| 307 | * @public
|
---|
| 308 | */
|
---|
| 309 | socketsJoin(room: Room | Room[]): void;
|
---|
| 310 | /**
|
---|
| 311 | * Makes the matching socket instances leave the specified rooms
|
---|
| 312 | *
|
---|
| 313 | * @param room
|
---|
| 314 | * @public
|
---|
| 315 | */
|
---|
| 316 | socketsLeave(room: Room | Room[]): void;
|
---|
| 317 | /**
|
---|
| 318 | * Makes the matching socket instances disconnect
|
---|
| 319 | *
|
---|
| 320 | * @param close - whether to close the underlying connection
|
---|
| 321 | * @public
|
---|
| 322 | */
|
---|
| 323 | disconnectSockets(close?: boolean): void;
|
---|
[6a3a178] | 324 | }
|
---|
[e29cc2e] | 325 | export { Socket, ServerOptions, Namespace, BroadcastOperator, RemoteSocket };
|
---|