HubConnection.d.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { IStreamResult } from "./Stream";
  2. /** Describes the current state of the {@link HubConnection} to the server. */
  3. export declare enum HubConnectionState {
  4. /** The hub connection is disconnected. */
  5. Disconnected = "Disconnected",
  6. /** The hub connection is connecting. */
  7. Connecting = "Connecting",
  8. /** The hub connection is connected. */
  9. Connected = "Connected",
  10. /** The hub connection is disconnecting. */
  11. Disconnecting = "Disconnecting",
  12. /** The hub connection is reconnecting. */
  13. Reconnecting = "Reconnecting"
  14. }
  15. /** Represents a connection to a SignalR Hub. */
  16. export declare class HubConnection {
  17. private readonly _cachedPingMessage;
  18. private readonly connection;
  19. private readonly _logger;
  20. private readonly _reconnectPolicy?;
  21. private _protocol;
  22. private _handshakeProtocol;
  23. private _callbacks;
  24. private _methods;
  25. private _invocationId;
  26. private _closedCallbacks;
  27. private _reconnectingCallbacks;
  28. private _reconnectedCallbacks;
  29. private _receivedHandshakeResponse;
  30. private _handshakeResolver;
  31. private _handshakeRejecter;
  32. private _stopDuringStartError?;
  33. private _connectionState;
  34. private _connectionStarted;
  35. private _startPromise?;
  36. private _stopPromise?;
  37. private _nextKeepAlive;
  38. private _reconnectDelayHandle?;
  39. private _timeoutHandle?;
  40. private _pingServerHandle?;
  41. private _freezeEventListener;
  42. /** The server timeout in milliseconds.
  43. *
  44. * If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error.
  45. * The default timeout value is 30,000 milliseconds (30 seconds).
  46. */
  47. serverTimeoutInMilliseconds: number;
  48. /** Default interval at which to ping the server.
  49. *
  50. * The default value is 15,000 milliseconds (15 seconds).
  51. * Allows the server to detect hard disconnects (like when a client unplugs their computer).
  52. * The ping will happen at most as often as the server pings.
  53. * If the server pings every 5 seconds, a value lower than 5 will ping every 5 seconds.
  54. */
  55. keepAliveIntervalInMilliseconds: number;
  56. private constructor();
  57. /** Indicates the state of the {@link HubConnection} to the server. */
  58. get state(): HubConnectionState;
  59. /** Represents the connection id of the {@link HubConnection} on the server. The connection id will be null when the connection is either
  60. * in the disconnected state or if the negotiation step was skipped.
  61. */
  62. get connectionId(): string | null;
  63. /** Indicates the url of the {@link HubConnection} to the server. */
  64. get baseUrl(): string;
  65. /**
  66. * Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or
  67. * Reconnecting states.
  68. * @param {string} url The url to connect to.
  69. */
  70. set baseUrl(url: string);
  71. /** Starts the connection.
  72. *
  73. * @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
  74. */
  75. start(): Promise<void>;
  76. private _startWithStateTransitions;
  77. private _startInternal;
  78. /** Stops the connection.
  79. *
  80. * @returns {Promise<void>} A Promise that resolves when the connection has been successfully terminated, or rejects with an error.
  81. */
  82. stop(): Promise<void>;
  83. private _stopInternal;
  84. /** Invokes a streaming hub method on the server using the specified name and arguments.
  85. *
  86. * @typeparam T The type of the items returned by the server.
  87. * @param {string} methodName The name of the server method to invoke.
  88. * @param {any[]} args The arguments used to invoke the server method.
  89. * @returns {IStreamResult<T>} An object that yields results from the server as they are received.
  90. */
  91. stream<T = any>(methodName: string, ...args: any[]): IStreamResult<T>;
  92. private _sendMessage;
  93. /**
  94. * Sends a js object to the server.
  95. * @param message The js object to serialize and send.
  96. */
  97. private _sendWithProtocol;
  98. /** Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver.
  99. *
  100. * The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still
  101. * be processing the invocation.
  102. *
  103. * @param {string} methodName The name of the server method to invoke.
  104. * @param {any[]} args The arguments used to invoke the server method.
  105. * @returns {Promise<void>} A Promise that resolves when the invocation has been successfully sent, or rejects with an error.
  106. */
  107. send(methodName: string, ...args: any[]): Promise<void>;
  108. /** Invokes a hub method on the server using the specified name and arguments.
  109. *
  110. * The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise
  111. * resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of
  112. * resolving the Promise.
  113. *
  114. * @typeparam T The expected return type.
  115. * @param {string} methodName The name of the server method to invoke.
  116. * @param {any[]} args The arguments used to invoke the server method.
  117. * @returns {Promise<T>} A Promise that resolves with the result of the server method (if any), or rejects with an error.
  118. */
  119. invoke<T = any>(methodName: string, ...args: any[]): Promise<T>;
  120. /** Registers a handler that will be invoked when the hub method with the specified method name is invoked.
  121. *
  122. * @param {string} methodName The name of the hub method to define.
  123. * @param {Function} newMethod The handler that will be raised when the hub method is invoked.
  124. */
  125. on(methodName: string, newMethod: (...args: any[]) => any): void;
  126. /** Removes all handlers for the specified hub method.
  127. *
  128. * @param {string} methodName The name of the method to remove handlers for.
  129. */
  130. off(methodName: string): void;
  131. /** Removes the specified handler for the specified hub method.
  132. *
  133. * You must pass the exact same Function instance as was previously passed to {@link @microsoft/signalr.HubConnection.on}. Passing a different instance (even if the function
  134. * body is the same) will not remove the handler.
  135. *
  136. * @param {string} methodName The name of the method to remove handlers for.
  137. * @param {Function} method The handler to remove. This must be the same Function instance as the one passed to {@link @microsoft/signalr.HubConnection.on}.
  138. */
  139. off(methodName: string, method: (...args: any[]) => void): void;
  140. /** Registers a handler that will be invoked when the connection is closed.
  141. *
  142. * @param {Function} callback The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any).
  143. */
  144. onclose(callback: (error?: Error) => void): void;
  145. /** Registers a handler that will be invoked when the connection starts reconnecting.
  146. *
  147. * @param {Function} callback The handler that will be invoked when the connection starts reconnecting. Optionally receives a single argument containing the error that caused the connection to start reconnecting (if any).
  148. */
  149. onreconnecting(callback: (error?: Error) => void): void;
  150. /** Registers a handler that will be invoked when the connection successfully reconnects.
  151. *
  152. * @param {Function} callback The handler that will be invoked when the connection successfully reconnects.
  153. */
  154. onreconnected(callback: (connectionId?: string) => void): void;
  155. private _processIncomingData;
  156. private _processHandshakeResponse;
  157. private _resetKeepAliveInterval;
  158. private _resetTimeoutPeriod;
  159. private serverTimeout;
  160. private _invokeClientMethod;
  161. private _connectionClosed;
  162. private _completeClose;
  163. private _reconnect;
  164. private _getNextRetryDelay;
  165. private _cancelCallbacksWithError;
  166. private _cleanupPingTimer;
  167. private _cleanupTimeout;
  168. private _createInvocation;
  169. private _launchStreams;
  170. private _replaceStreamingParams;
  171. private _isObservable;
  172. private _createStreamInvocation;
  173. private _createCancelInvocation;
  174. private _createStreamItemMessage;
  175. private _createCompletionMessage;
  176. }