JsonHubProtocol.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // 版权所有(c).NET基金会。保留所有权利。
  2. // 在2.0版Apache许可下授权。有关许可证信息,请参见项目根目录中的License.txt。
  3. import { MessageType } from "./IHubProtocol";
  4. import { LogLevel } from "./ILogger";
  5. import { TransferFormat } from "./ITransport";
  6. import { NullLogger } from "./Loggers";
  7. import { TextMessageFormat } from "./TextMessageFormat";
  8. var JSON_HUB_PROTOCOL_NAME = "json";
  9. /** Implements the JSON Hub Protocol. */
  10. var JsonHubProtocol = /** @class */ (function () {
  11. function JsonHubProtocol() {
  12. /** @inheritDoc */
  13. this.name = JSON_HUB_PROTOCOL_NAME;
  14. /** @inheritDoc */
  15. this.version = 1;
  16. /** @inheritDoc */
  17. this.transferFormat = TransferFormat.Text;
  18. }
  19. /**
  20. * Creates an array of {@link @aspnet/signalr.HubMessage} objects from the specified serialized representation.
  21. * 从指定的序列化表示创建{@link@aspnet/signaler.HubMessage}对象数组
  22. *
  23. * @param {string} input 包含序列化表示的字符串 A string containing the serialized representation.
  24. * @param {ILogger} logger
  25. */
  26. JsonHubProtocol.prototype.parseMessages = function (input, logger) {
  27. // 接口允许传入“ArrayBuffer”,但此实现不允许。所以让我们抛出一个有用的错误
  28. if (typeof input !== "string") {
  29. throw new Error("Invalid input for JSON hub protocol. Expected a string. (fy:包含一个无效的JSON协议输入,但是这里需要输入string 消息)");
  30. }
  31. // fixed
  32. if (!input) {
  33. return [];
  34. }
  35. // fixed
  36. if (logger === null) {
  37. logger = NullLogger.instance;
  38. }
  39. // string 类型消息格式化转换
  40. var messages = TextMessageFormat.parse(input);
  41. var hubMessages = [];
  42. for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
  43. var message = messages_1[_i];
  44. // 转换消息
  45. var parsedMessage = JSON.parse(message);
  46. if (typeof parsedMessage.type !== "number") {
  47. throw new Error("Invalid payload. (fy: 无效的消息)");
  48. }
  49. switch (parsedMessage.type) {
  50. case MessageType.Invocation: // 调用命令
  51. this.isInvocationMessage(parsedMessage);
  52. break;
  53. case MessageType.StreamItem: // 流消息
  54. this.isStreamItemMessage(parsedMessage);
  55. break;
  56. case MessageType.Completion: // 完成消息
  57. this.isCompletionMessage(parsedMessage);
  58. break;
  59. case MessageType.Ping: // ping 命令
  60. // Single value, no need to validate
  61. break;
  62. case MessageType.Close: // 关闭命令
  63. // All optional values, no need to validate
  64. break;
  65. default: // 未定义命令,抛出异常
  66. // Future protocol changes can add message types, old clients can ignore them
  67. logger.log(LogLevel.Information, "Unknown message type '" + parsedMessage.type + "' ignored.");
  68. continue;
  69. }
  70. hubMessages.push(parsedMessage);
  71. }
  72. return hubMessages;
  73. };
  74. /**
  75. * Writes the specified {@link @aspnet/signalr.HubMessage} to a string and returns it.
  76. * 将指定的{@link@aspnet/signalr.HubMessage}写入字符串并返回
  77. *
  78. * @param {HubMessage} message The message to write. 消息内容
  79. * @returns {string} 包含消息的序列化表示形式的字符串。
  80. */
  81. JsonHubProtocol.prototype.writeMessage = function (message) {
  82. return TextMessageFormat.write(JSON.stringify(message));
  83. };
  84. /**
  85. * 判断是否是一个正常的调用消息
  86. * @param message
  87. */
  88. JsonHubProtocol.prototype.isInvocationMessage = function (message) {
  89. this.assertNotEmptyString(message.target, "Invalid payload for Invocation message.");
  90. if (message.invocationId !== undefined) {
  91. this.assertNotEmptyString(message.invocationId, "Invalid payload for Invocation message. (fy:无效的 [调用] 消息)");
  92. }
  93. };
  94. /**
  95. * 判断是否是一个流消息子项
  96. * @param message
  97. */
  98. JsonHubProtocol.prototype.isStreamItemMessage = function (message) {
  99. this.assertNotEmptyString(message.invocationId, "Invalid payload for StreamItem message. (fy:无效的 [StreamItem] 消息)");
  100. if (message.item === undefined) {
  101. throw new Error("Invalid payload for StreamItem message. (fy:无效的 [StreamItem] 消息)");
  102. }
  103. };
  104. /**
  105. * 判断是否是一个完整的消息
  106. * @param message
  107. */
  108. JsonHubProtocol.prototype.isCompletionMessage = function (message) {
  109. if (message.result && message.error) {
  110. throw new Error("Invalid payload for Completion message (fy:消息不完整).");
  111. }
  112. if (!message.result && message.error) {
  113. this.assertNotEmptyString(message.error, "Invalid payload for Completion message (fy:消息不完整).");
  114. }
  115. this.assertNotEmptyString(message.invocationId, "Invalid payload for Completion message (fy:消息不完整).");
  116. };
  117. /**
  118. * 断言非空字符串
  119. * @param value
  120. * @param errorMessage
  121. */
  122. JsonHubProtocol.prototype.assertNotEmptyString = function (value, errorMessage) {
  123. if (typeof value !== "string" || value === "") {
  124. throw new Error(errorMessage);
  125. }
  126. };
  127. return JsonHubProtocol;
  128. }());
  129. export { JsonHubProtocol };