UNPKG

5.24 kBTypeScriptView Raw
1/*!
2 * Reconnecting WebSocket
3 * by Pedro Ladaria <pedro.ladaria@gmail.com>
4 * https://github.com/pladaria/reconnecting-websocket
5 * License MIT
6 */
7import * as Events from './events';
8export declare type Event = Events.Event;
9export declare type ErrorEvent = Events.ErrorEvent;
10export declare type CloseEvent = Events.CloseEvent;
11export declare type Options = {
12 WebSocket?: any;
13 maxReconnectionDelay?: number;
14 minReconnectionDelay?: number;
15 reconnectionDelayGrowFactor?: number;
16 minUptime?: number;
17 connectionTimeout?: number;
18 maxRetries?: number;
19 maxEnqueuedMessages?: number;
20 startClosed?: boolean;
21 debug?: boolean;
22};
23export declare type UrlProvider = string | (() => string) | (() => Promise<string>);
24export declare type Message = string | ArrayBuffer | Blob | ArrayBufferView;
25export declare type ListenersMap = {
26 error: Array<Events.WebSocketEventListenerMap['error']>;
27 message: Array<Events.WebSocketEventListenerMap['message']>;
28 open: Array<Events.WebSocketEventListenerMap['open']>;
29 close: Array<Events.WebSocketEventListenerMap['close']>;
30};
31export default class ReconnectingWebSocket {
32 private _ws?;
33 private _listeners;
34 private _retryCount;
35 private _uptimeTimeout;
36 private _connectTimeout;
37 private _shouldReconnect;
38 private _connectLock;
39 private _binaryType;
40 private _closeCalled;
41 private _messageQueue;
42 private readonly _url;
43 private readonly _protocols?;
44 private readonly _options;
45 constructor(url: UrlProvider, protocols?: string | string[], options?: Options);
46 static readonly CONNECTING: number;
47 static readonly OPEN: number;
48 static readonly CLOSING: number;
49 static readonly CLOSED: number;
50 readonly CONNECTING: number;
51 readonly OPEN: number;
52 readonly CLOSING: number;
53 readonly CLOSED: number;
54 binaryType: BinaryType;
55 /**
56 * Returns the number or connection retries
57 */
58 readonly retryCount: number;
59 /**
60 * The number of bytes of data that have been queued using calls to send() but not yet
61 * transmitted to the network. This value resets to zero once all queued data has been sent.
62 * This value does not reset to zero when the connection is closed; if you keep calling send(),
63 * this will continue to climb. Read only
64 */
65 readonly bufferedAmount: number;
66 /**
67 * The extensions selected by the server. This is currently only the empty string or a list of
68 * extensions as negotiated by the connection
69 */
70 readonly extensions: string;
71 /**
72 * A string indicating the name of the sub-protocol the server selected;
73 * this will be one of the strings specified in the protocols parameter when creating the
74 * WebSocket object
75 */
76 readonly protocol: string;
77 /**
78 * The current state of the connection; this is one of the Ready state constants
79 */
80 readonly readyState: number;
81 /**
82 * The URL as resolved by the constructor
83 */
84 readonly url: string;
85 /**
86 * An event listener to be called when the WebSocket connection's readyState changes to CLOSED
87 */
88 onclose: ((event: Events.CloseEvent) => void) | null;
89 /**
90 * An event listener to be called when an error occurs
91 */
92 onerror: ((event: Events.ErrorEvent) => void) | null;
93 /**
94 * An event listener to be called when a message is received from the server
95 */
96 onmessage: ((event: MessageEvent) => void) | null;
97 /**
98 * An event listener to be called when the WebSocket connection's readyState changes to OPEN;
99 * this indicates that the connection is ready to send and receive data
100 */
101 onopen: ((event: Event) => void) | null;
102 /**
103 * Closes the WebSocket connection or connection attempt, if any. If the connection is already
104 * CLOSED, this method does nothing
105 */
106 close(code?: number, reason?: string): void;
107 /**
108 * Closes the WebSocket connection or connection attempt and connects again.
109 * Resets retry counter;
110 */
111 reconnect(code?: number, reason?: string): void;
112 /**
113 * Enqueue specified data to be transmitted to the server over the WebSocket connection
114 */
115 send(data: Message): void;
116 /**
117 * Register an event handler of a specific event type
118 */
119 addEventListener<T extends keyof Events.WebSocketEventListenerMap>(type: T, listener: Events.WebSocketEventListenerMap[T]): void;
120 dispatchEvent(event: Event): boolean;
121 /**
122 * Removes an event listener
123 */
124 removeEventListener<T extends keyof Events.WebSocketEventListenerMap>(type: T, listener: Events.WebSocketEventListenerMap[T]): void;
125 private _debug;
126 private _getNextDelay;
127 private _wait;
128 private _getNextUrl;
129 private _connect;
130 private _handleTimeout;
131 private _disconnect;
132 private _acceptOpen;
133 private _callEventListener;
134 private _handleOpen;
135 private _handleMessage;
136 private _handleError;
137 private _handleClose;
138 private _removeListeners;
139 private _addListeners;
140 private _clearTimeouts;
141}
142
\No newline at end of file