IHubProtocol.d.ts 6.8 KB

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