UNPKG

7.11 kBTypeScriptView Raw
1import { Socket as Engine, SocketOptions as EngineOptions } from "engine.io-client";
2import { Socket, SocketOptions } 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) => 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 _reconnection;
99 private _reconnectionAttempts;
100 private _reconnectionDelay;
101 private _randomizationFactor;
102 private _reconnectionDelayMax;
103 private _timeout;
104 private encoder;
105 private decoder;
106 private skipReconnect;
107 /**
108 * `Manager` constructor.
109 *
110 * @param uri - engine instance or engine uri/opts
111 * @param opts - options
112 * @public
113 */
114 constructor(opts: Partial<ManagerOptions>);
115 constructor(uri?: string, opts?: Partial<ManagerOptions>);
116 constructor(uri?: string | Partial<ManagerOptions>, opts?: Partial<ManagerOptions>);
117 /**
118 * Sets the `reconnection` config.
119 *
120 * @param {Boolean} v - true/false if it should automatically reconnect
121 * @return {Manager} self or value
122 * @public
123 */
124 reconnection(v: boolean): this;
125 reconnection(): boolean;
126 reconnection(v?: boolean): this | boolean;
127 /**
128 * Sets the reconnection attempts config.
129 *
130 * @param {Number} v - max reconnection attempts before giving up
131 * @return {Manager} self or value
132 * @public
133 */
134 reconnectionAttempts(v: number): this;
135 reconnectionAttempts(): number;
136 reconnectionAttempts(v?: number): this | number;
137 /**
138 * Sets the delay between reconnections.
139 *
140 * @param {Number} v - delay
141 * @return {Manager} self or value
142 * @public
143 */
144 reconnectionDelay(v: number): this;
145 reconnectionDelay(): number;
146 reconnectionDelay(v?: number): this | number;
147 /**
148 * Sets the randomization factor
149 *
150 * @param v - the randomization factor
151 * @return self or value
152 * @public
153 */
154 randomizationFactor(v: number): this;
155 randomizationFactor(): number;
156 randomizationFactor(v?: number): this | number;
157 /**
158 * Sets the maximum delay between reconnections.
159 *
160 * @param v - delay
161 * @return self or value
162 * @public
163 */
164 reconnectionDelayMax(v: number): this;
165 reconnectionDelayMax(): number;
166 reconnectionDelayMax(v?: number): this | number;
167 /**
168 * Sets the connection timeout. `false` to disable
169 *
170 * @param v - connection timeout
171 * @return self or value
172 * @public
173 */
174 timeout(v: number | boolean): this;
175 timeout(): number | boolean;
176 timeout(v?: number | boolean): this | number | boolean;
177 /**
178 * Starts trying to reconnect if reconnection is enabled and we have not
179 * started reconnecting yet
180 *
181 * @private
182 */
183 private maybeReconnectOnOpen;
184 /**
185 * Sets the current transport `socket`.
186 *
187 * @param {Function} fn - optional, callback
188 * @return self
189 * @public
190 */
191 open(fn?: (err?: Error) => void): this;
192 /**
193 * Alias for open()
194 *
195 * @return self
196 * @public
197 */
198 connect(fn?: (err?: Error) => void): this;
199 /**
200 * Called upon transport open.
201 *
202 * @private
203 */
204 private onopen;
205 /**
206 * Called upon a ping.
207 *
208 * @private
209 */
210 private onping;
211 /**
212 * Called with data.
213 *
214 * @private
215 */
216 private ondata;
217 /**
218 * Called when parser fully decodes a packet.
219 *
220 * @private
221 */
222 private ondecoded;
223 /**
224 * Called upon socket error.
225 *
226 * @private
227 */
228 private onerror;
229 /**
230 * Creates a new socket for the given `nsp`.
231 *
232 * @return {Socket}
233 * @public
234 */
235 socket(nsp: string, opts?: Partial<SocketOptions>): Socket;
236 /**
237 * Called upon a socket close.
238 *
239 * @param socket
240 * @private
241 */
242 _destroy(socket: Socket): void;
243 /**
244 * Writes a packet.
245 *
246 * @param packet
247 * @private
248 */
249 _packet(packet: Partial<Packet & {
250 query: string;
251 options: any;
252 }>): void;
253 /**
254 * Clean up transport subscriptions and packet buffer.
255 *
256 * @private
257 */
258 private cleanup;
259 /**
260 * Close the current socket.
261 *
262 * @private
263 */
264 _close(): void;
265 /**
266 * Alias for close()
267 *
268 * @private
269 */
270 private disconnect;
271 /**
272 * Called upon engine close.
273 *
274 * @private
275 */
276 private onclose;
277 /**
278 * Attempt a reconnection.
279 *
280 * @private
281 */
282 private reconnect;
283 /**
284 * Called upon successful reconnect.
285 *
286 * @private
287 */
288 private onreconnect;
289}
290export {};