manager.d.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { Socket as Engine, SocketOptions as EngineOptions } from "engine.io-client";
  2. import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
  3. import { Packet } from "socket.io-parser";
  4. import { DefaultEventsMap, EventsMap, Emitter } from "@socket.io/component-emitter";
  5. export interface ManagerOptions extends EngineOptions {
  6. /**
  7. * Should we force a new Manager for this connection?
  8. * @default false
  9. */
  10. forceNew: boolean;
  11. /**
  12. * Should we multiplex our connection (reuse existing Manager) ?
  13. * @default true
  14. */
  15. multiplex: boolean;
  16. /**
  17. * The path to get our client file from, in the case of the server
  18. * serving it
  19. * @default '/socket.io'
  20. */
  21. path: string;
  22. /**
  23. * Should we allow reconnections?
  24. * @default true
  25. */
  26. reconnection: boolean;
  27. /**
  28. * How many reconnection attempts should we try?
  29. * @default Infinity
  30. */
  31. reconnectionAttempts: number;
  32. /**
  33. * The time delay in milliseconds between reconnection attempts
  34. * @default 1000
  35. */
  36. reconnectionDelay: number;
  37. /**
  38. * The max time delay in milliseconds between reconnection attempts
  39. * @default 5000
  40. */
  41. reconnectionDelayMax: number;
  42. /**
  43. * Used in the exponential backoff jitter when reconnecting
  44. * @default 0.5
  45. */
  46. randomizationFactor: number;
  47. /**
  48. * The timeout in milliseconds for our connection attempt
  49. * @default 20000
  50. */
  51. timeout: number;
  52. /**
  53. * Should we automatically connect?
  54. * @default true
  55. */
  56. autoConnect: boolean;
  57. /**
  58. * the parser to use. Defaults to an instance of the Parser that ships with socket.io.
  59. */
  60. parser: any;
  61. }
  62. interface ManagerReservedEvents {
  63. open: () => void;
  64. error: (err: Error) => void;
  65. ping: () => void;
  66. packet: (packet: Packet) => void;
  67. close: (reason: string, description?: DisconnectDescription) => void;
  68. reconnect_failed: () => void;
  69. reconnect_attempt: (attempt: number) => void;
  70. reconnect_error: (err: Error) => void;
  71. reconnect: (attempt: number) => void;
  72. }
  73. export declare class Manager<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> extends Emitter<{}, {}, ManagerReservedEvents> {
  74. /**
  75. * The Engine.IO client instance
  76. *
  77. * @public
  78. */
  79. engine: Engine;
  80. /**
  81. * @private
  82. */
  83. _autoConnect: boolean;
  84. /**
  85. * @private
  86. */
  87. _readyState: "opening" | "open" | "closed";
  88. /**
  89. * @private
  90. */
  91. _reconnecting: boolean;
  92. private readonly uri;
  93. opts: Partial<ManagerOptions>;
  94. private nsps;
  95. private subs;
  96. private backoff;
  97. private setTimeoutFn;
  98. private clearTimeoutFn;
  99. private _reconnection;
  100. private _reconnectionAttempts;
  101. private _reconnectionDelay;
  102. private _randomizationFactor;
  103. private _reconnectionDelayMax;
  104. private _timeout;
  105. private encoder;
  106. private decoder;
  107. private skipReconnect;
  108. /**
  109. * `Manager` constructor.
  110. *
  111. * @param uri - engine instance or engine uri/opts
  112. * @param opts - options
  113. * @public
  114. */
  115. constructor(opts: Partial<ManagerOptions>);
  116. constructor(uri?: string, opts?: Partial<ManagerOptions>);
  117. constructor(uri?: string | Partial<ManagerOptions>, opts?: Partial<ManagerOptions>);
  118. /**
  119. * Sets the `reconnection` config.
  120. *
  121. * @param {Boolean} v - true/false if it should automatically reconnect
  122. * @return {Manager} self or value
  123. * @public
  124. */
  125. reconnection(v: boolean): this;
  126. reconnection(): boolean;
  127. reconnection(v?: boolean): this | boolean;
  128. /**
  129. * Sets the reconnection attempts config.
  130. *
  131. * @param {Number} v - max reconnection attempts before giving up
  132. * @return {Manager} self or value
  133. * @public
  134. */
  135. reconnectionAttempts(v: number): this;
  136. reconnectionAttempts(): number;
  137. reconnectionAttempts(v?: number): this | number;
  138. /**
  139. * Sets the delay between reconnections.
  140. *
  141. * @param {Number} v - delay
  142. * @return {Manager} self or value
  143. * @public
  144. */
  145. reconnectionDelay(v: number): this;
  146. reconnectionDelay(): number;
  147. reconnectionDelay(v?: number): this | number;
  148. /**
  149. * Sets the randomization factor
  150. *
  151. * @param v - the randomization factor
  152. * @return self or value
  153. * @public
  154. */
  155. randomizationFactor(v: number): this;
  156. randomizationFactor(): number;
  157. randomizationFactor(v?: number): this | number;
  158. /**
  159. * Sets the maximum delay between reconnections.
  160. *
  161. * @param v - delay
  162. * @return self or value
  163. * @public
  164. */
  165. reconnectionDelayMax(v: number): this;
  166. reconnectionDelayMax(): number;
  167. reconnectionDelayMax(v?: number): this | number;
  168. /**
  169. * Sets the connection timeout. `false` to disable
  170. *
  171. * @param v - connection timeout
  172. * @return self or value
  173. * @public
  174. */
  175. timeout(v: number | boolean): this;
  176. timeout(): number | boolean;
  177. timeout(v?: number | boolean): this | number | boolean;
  178. /**
  179. * Starts trying to reconnect if reconnection is enabled and we have not
  180. * started reconnecting yet
  181. *
  182. * @private
  183. */
  184. private maybeReconnectOnOpen;
  185. /**
  186. * Sets the current transport `socket`.
  187. *
  188. * @param {Function} fn - optional, callback
  189. * @return self
  190. * @public
  191. */
  192. open(fn?: (err?: Error) => void): this;
  193. /**
  194. * Alias for open()
  195. *
  196. * @return self
  197. * @public
  198. */
  199. connect(fn?: (err?: Error) => void): this;
  200. /**
  201. * Called upon transport open.
  202. *
  203. * @private
  204. */
  205. private onopen;
  206. /**
  207. * Called upon a ping.
  208. *
  209. * @private
  210. */
  211. private onping;
  212. /**
  213. * Called with data.
  214. *
  215. * @private
  216. */
  217. private ondata;
  218. /**
  219. * Called when parser fully decodes a packet.
  220. *
  221. * @private
  222. */
  223. private ondecoded;
  224. /**
  225. * Called upon socket error.
  226. *
  227. * @private
  228. */
  229. private onerror;
  230. /**
  231. * Creates a new socket for the given `nsp`.
  232. *
  233. * @return {Socket}
  234. * @public
  235. */
  236. socket(nsp: string, opts?: Partial<SocketOptions>): Socket;
  237. /**
  238. * Called upon a socket close.
  239. *
  240. * @param socket
  241. * @private
  242. */
  243. _destroy(socket: Socket): void;
  244. /**
  245. * Writes a packet.
  246. *
  247. * @param packet
  248. * @private
  249. */
  250. _packet(packet: Partial<Packet & {
  251. query: string;
  252. options: any;
  253. }>): void;
  254. /**
  255. * Clean up transport subscriptions and packet buffer.
  256. *
  257. * @private
  258. */
  259. private cleanup;
  260. /**
  261. * Close the current socket.
  262. *
  263. * @private
  264. */
  265. _close(): void;
  266. /**
  267. * Alias for close()
  268. *
  269. * @private
  270. */
  271. private disconnect;
  272. /**
  273. * Called upon engine close.
  274. *
  275. * @private
  276. */
  277. private onclose;
  278. /**
  279. * Attempt a reconnection.
  280. *
  281. * @private
  282. */
  283. private reconnect;
  284. /**
  285. * Called upon successful reconnect.
  286. *
  287. * @private
  288. */
  289. private onreconnect;
  290. }
  291. export {};