IHubProtocol.d.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { ILogger } from "./ILogger";
  2. import { TransferFormat } from "./ITransport";
  3. /** Defines the type of a Hub Message. */
  4. export declare enum MessageType {
  5. /** Indicates the message is an Invocation message and implements the {@link @microsoft/signalr.InvocationMessage} interface. */
  6. Invocation = 1,
  7. /** Indicates the message is a StreamItem message and implements the {@link @microsoft/signalr.StreamItemMessage} interface. */
  8. StreamItem = 2,
  9. /** Indicates the message is a Completion message and implements the {@link @microsoft/signalr.CompletionMessage} interface. */
  10. Completion = 3,
  11. /** Indicates the message is a Stream Invocation message and implements the {@link @microsoft/signalr.StreamInvocationMessage} interface. */
  12. StreamInvocation = 4,
  13. /** Indicates the message is a Cancel Invocation message and implements the {@link @microsoft/signalr.CancelInvocationMessage} interface. */
  14. CancelInvocation = 5,
  15. /** Indicates the message is a Ping message and implements the {@link @microsoft/signalr.PingMessage} interface. */
  16. Ping = 6,
  17. /** Indicates the message is a Close message and implements the {@link @microsoft/signalr.CloseMessage} interface. */
  18. Close = 7
  19. }
  20. /** Defines a dictionary of string keys and string values representing headers attached to a Hub message. */
  21. export interface MessageHeaders {
  22. /** Gets or sets the header with the specified key. */
  23. [key: string]: string;
  24. }
  25. /** Union type of all known Hub messages. */
  26. export declare type HubMessage = InvocationMessage | StreamInvocationMessage | StreamItemMessage | CompletionMessage | CancelInvocationMessage | PingMessage | CloseMessage;
  27. /** Defines properties common to all Hub messages. */
  28. export interface HubMessageBase {
  29. /** A {@link @microsoft/signalr.MessageType} value indicating the type of this message. */
  30. readonly type: MessageType;
  31. }
  32. /** Defines properties common to all Hub messages relating to a specific invocation. */
  33. export interface HubInvocationMessage extends HubMessageBase {
  34. /** A {@link @microsoft/signalr.MessageHeaders} dictionary containing headers attached to the message. */
  35. readonly headers?: MessageHeaders;
  36. /** The ID of the invocation relating to this message.
  37. *
  38. * This is expected to be present for {@link @microsoft/signalr.StreamInvocationMessage} and {@link @microsoft/signalr.CompletionMessage}. It may
  39. * be 'undefined' for an {@link @microsoft/signalr.InvocationMessage} if the sender does not expect a response.
  40. */
  41. readonly invocationId?: string;
  42. }
  43. /** A hub message representing a non-streaming invocation. */
  44. export interface InvocationMessage extends HubInvocationMessage {
  45. /** @inheritDoc */
  46. readonly type: MessageType.Invocation;
  47. /** The target method name. */
  48. readonly target: string;
  49. /** The target method arguments. */
  50. readonly arguments: any[];
  51. /** The target methods stream IDs. */
  52. readonly streamIds?: string[];
  53. }
  54. /** A hub message representing a streaming invocation. */
  55. export interface StreamInvocationMessage extends HubInvocationMessage {
  56. /** @inheritDoc */
  57. readonly type: MessageType.StreamInvocation;
  58. /** The invocation ID. */
  59. readonly invocationId: string;
  60. /** The target method name. */
  61. readonly target: string;
  62. /** The target method arguments. */
  63. readonly arguments: any[];
  64. /** The target methods stream IDs. */
  65. readonly streamIds?: string[];
  66. }
  67. /** A hub message representing a single item produced as part of a result stream. */
  68. export interface StreamItemMessage extends HubInvocationMessage {
  69. /** @inheritDoc */
  70. readonly type: MessageType.StreamItem;
  71. /** The invocation ID. */
  72. readonly invocationId: string;
  73. /** The item produced by the server. */
  74. readonly item?: any;
  75. }
  76. /** A hub message representing the result of an invocation. */
  77. export interface CompletionMessage extends HubInvocationMessage {
  78. /** @inheritDoc */
  79. readonly type: MessageType.Completion;
  80. /** The invocation ID. */
  81. readonly invocationId: string;
  82. /** The error produced by the invocation, if any.
  83. *
  84. * Either {@link @microsoft/signalr.CompletionMessage.error} or {@link @microsoft/signalr.CompletionMessage.result} must be defined, but not both.
  85. */
  86. readonly error?: string;
  87. /** The result produced by the invocation, if any.
  88. *
  89. * Either {@link @microsoft/signalr.CompletionMessage.error} or {@link @microsoft/signalr.CompletionMessage.result} must be defined, but not both.
  90. */
  91. readonly result?: any;
  92. }
  93. /** A hub message indicating that the sender is still active. */
  94. export interface PingMessage extends HubMessageBase {
  95. /** @inheritDoc */
  96. readonly type: MessageType.Ping;
  97. }
  98. /** A hub message indicating that the sender is closing the connection.
  99. *
  100. * If {@link @microsoft/signalr.CloseMessage.error} is defined, the sender is closing the connection due to an error.
  101. */
  102. export interface CloseMessage extends HubMessageBase {
  103. /** @inheritDoc */
  104. readonly type: MessageType.Close;
  105. /** The error that triggered the close, if any.
  106. *
  107. * If this property is undefined, the connection was closed normally and without error.
  108. */
  109. readonly error?: string;
  110. /** If true, clients with automatic reconnects enabled should attempt to reconnect after receiving the CloseMessage. Otherwise, they should not. */
  111. readonly allowReconnect?: boolean;
  112. }
  113. /** A hub message sent to request that a streaming invocation be canceled. */
  114. export interface CancelInvocationMessage extends HubInvocationMessage {
  115. /** @inheritDoc */
  116. readonly type: MessageType.CancelInvocation;
  117. /** The invocation ID. */
  118. readonly invocationId: string;
  119. }
  120. /** A protocol abstraction for communicating with SignalR Hubs. */
  121. export interface IHubProtocol {
  122. /** The name of the protocol. This is used by SignalR to resolve the protocol between the client and server. */
  123. readonly name: string;
  124. /** The version of the protocol. */
  125. readonly version: number;
  126. /** The {@link @microsoft/signalr.TransferFormat} of the protocol. */
  127. readonly transferFormat: TransferFormat;
  128. /** Creates an array of {@link @microsoft/signalr.HubMessage} objects from the specified serialized representation.
  129. *
  130. * If {@link @microsoft/signalr.IHubProtocol.transferFormat} is 'Text', the `input` parameter must be a string, otherwise it must be an ArrayBuffer.
  131. *
  132. * @param {string | ArrayBuffer} input A string or ArrayBuffer containing the serialized representation.
  133. * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.
  134. */
  135. parseMessages(input: string | ArrayBuffer, logger: ILogger): HubMessage[];
  136. /** Writes the specified {@link @microsoft/signalr.HubMessage} to a string or ArrayBuffer and returns it.
  137. *
  138. * If {@link @microsoft/signalr.IHubProtocol.transferFormat} is 'Text', the result of this method will be a string, otherwise it will be an ArrayBuffer.
  139. *
  140. * @param {HubMessage} message The message to write.
  141. * @returns {string | ArrayBuffer} A string or ArrayBuffer containing the serialized representation of the message.
  142. */
  143. writeMessage(message: HubMessage): string | ArrayBuffer;
  144. }