reconnecting-websocket.d.ts 4.7 KB

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