HubConnection.d.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = 0,
  6. /** The hub connection is connected. */
  7. Connected = 1
  8. }
  9. /** Represents a connection to a SignalR Hub. */
  10. export declare class HubConnection {
  11. private readonly cachedPingMessage;
  12. private readonly connection;
  13. private readonly logger;
  14. private protocol;
  15. private handshakeProtocol;
  16. private callbacks;
  17. private methods;
  18. private id;
  19. private closedCallbacks;
  20. private receivedHandshakeResponse;
  21. private handshakeResolver;
  22. private handshakeRejecter;
  23. private connectionState;
  24. private timeoutHandle?;
  25. private pingServerHandle?;
  26. /** The server timeout in milliseconds.
  27. *
  28. * If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error.
  29. * The default timeout value is 30,000 milliseconds (30 seconds).
  30. */
  31. serverTimeoutInMilliseconds: number;
  32. /** Default interval at which to ping the server.
  33. *
  34. * The default value is 15,000 milliseconds (15 seconds).
  35. * Allows the server to detect hard disconnects (like when a client unplugs their computer).
  36. */
  37. keepAliveIntervalInMilliseconds: number;
  38. private constructor();
  39. /** Indicates the state of the {@link HubConnection} to the server. */
  40. readonly state: HubConnectionState;
  41. /** Starts the connection.
  42. *
  43. * @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
  44. */
  45. start(): Promise<void>;
  46. /** Stops the connection.
  47. *
  48. * @returns {Promise<void>} A Promise that resolves when the connection has been successfully terminated, or rejects with an error.
  49. */
  50. stop(): Promise<void>;
  51. /** Invokes a streaming hub method on the server using the specified name and arguments.
  52. *
  53. * @typeparam T The type of the items returned by the server.
  54. * @param {string} methodName The name of the server method to invoke.
  55. * @param {any[]} args The arguments used to invoke the server method.
  56. * @returns {IStreamResult<T>} An object that yields results from the server as they are received.
  57. */
  58. stream<T = any>(methodName: string, ...args: any[]): IStreamResult<T>;
  59. private sendMessage;
  60. /** Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver.
  61. *
  62. * The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still
  63. * be processing the invocation.
  64. *
  65. * @param {string} methodName The name of the server method to invoke.
  66. * @param {any[]} args The arguments used to invoke the server method.
  67. * @returns {Promise<void>} A Promise that resolves when the invocation has been successfully sent, or rejects with an error.
  68. */
  69. send(methodName: string, ...args: any[]): Promise<void>;
  70. /** Invokes a hub method on the server using the specified name and arguments.
  71. *
  72. * The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise
  73. * resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of
  74. * resolving the Promise.
  75. *
  76. * @typeparam T The expected return type.
  77. * @param {string} methodName The name of the server method to invoke.
  78. * @param {any[]} args The arguments used to invoke the server method.
  79. * @returns {Promise<T>} A Promise that resolves with the result of the server method (if any), or rejects with an error.
  80. */
  81. invoke<T = any>(methodName: string, ...args: any[]): Promise<T>;
  82. /** Registers a handler that will be invoked when the hub method with the specified method name is invoked.
  83. *
  84. * @param {string} methodName The name of the hub method to define.
  85. * @param {Function} newMethod The handler that will be raised when the hub method is invoked.
  86. */
  87. on(methodName: string, newMethod: (...args: any[]) => void): void;
  88. /** Removes all handlers for the specified hub method.
  89. *
  90. * @param {string} methodName The name of the method to remove handlers for.
  91. */
  92. off(methodName: string): void;
  93. /** Removes the specified handler for the specified hub method.
  94. *
  95. * You must pass the exact same Function instance as was previously passed to {@link @aspnet/signalr.HubConnection.on}. Passing a different instance (even if the function
  96. * body is the same) will not remove the handler.
  97. *
  98. * @param {string} methodName The name of the method to remove handlers for.
  99. * @param {Function} method The handler to remove. This must be the same Function instance as the one passed to {@link @aspnet/signalr.HubConnection.on}.
  100. */
  101. off(methodName: string, method: (...args: any[]) => void): void;
  102. /** Registers a handler that will be invoked when the connection is closed.
  103. *
  104. * @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).
  105. */
  106. onclose(callback: (error?: Error) => void): void;
  107. private processIncomingData;
  108. private processHandshakeResponse;
  109. private resetKeepAliveInterval;
  110. private resetTimeoutPeriod;
  111. private serverTimeout;
  112. private invokeClientMethod;
  113. private connectionClosed;
  114. private cleanupPingTimer;
  115. private cleanupTimeout;
  116. private createInvocation;
  117. private createStreamInvocation;
  118. private createCancelInvocation;
  119. }