index.d.ts 10 KB

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