UNPKG

7.33 kBTypeScriptView Raw
1import { Socket as Engine, SocketOptions as EngineOptions } from "engine.io-client";
2import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
3import { Packet } from "socket.io-parser";
4import { DefaultEventsMap, EventsMap, Emitter } from "@socket.io/component-emitter";
5export 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}
62interface 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}
73export 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 when:
274 *
275 * - the low-level engine is closed
276 * - the parser encountered a badly formatted packet
277 * - all sockets are disconnected
278 *
279 * @private
280 */
281 private onclose;
282 /**
283 * Attempt a reconnection.
284 *
285 * @private
286 */
287 private reconnect;
288 /**
289 * Called upon successful reconnect.
290 *
291 * @private
292 */
293 private onreconnect;
294}
295export {};