HandshakeProtocol.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // 版权所有(c).NET基金会。保留所有权利。
  2. // 在2.0版Apache许可下授权。有关许可证信息,请参见项目根目录中的License.txt。
  3. import { TextMessageFormat } from "./TextMessageFormat";
  4. import { isArrayBuffer } from "./Utils";
  5. /**
  6. * 握手协议
  7. * @private
  8. */
  9. var HandshakeProtocol = /** @class */ (function () {
  10. function HandshakeProtocol() {
  11. }
  12. // Handshake request is always JSON - 握手请求总是JSON
  13. HandshakeProtocol.prototype.writeHandshakeRequest = function (handshakeRequest) {
  14. // commond
  15. return TextMessageFormat.write(JSON.stringify(handshakeRequest));
  16. };
  17. /**
  18. * 解析握手协议 response
  19. *
  20. * @param {*} data
  21. * @returns {[any, HandshakeResponseMessage]}
  22. * @memberof HandshakeProtocol
  23. */
  24. HandshakeProtocol.prototype.parseHandshakeResponse = function (data) {
  25. var responseMessage;
  26. var messageData;
  27. var remainingData;
  28. // ! 删除了 @aspnet/signlaR 原有的 Buffer(仅适用 nodejs)判断
  29. if (isArrayBuffer(data)) {
  30. // Format is binary but still need to read JSON text from handshake response - fy: 格式是二进制的,但仍然需要从握手响应中读取JSON文本
  31. var binaryData = new Uint8Array(data);
  32. var separatorIndex = binaryData.indexOf(TextMessageFormat.RecordSeparatorCode);
  33. if (separatorIndex === -1) {
  34. throw new Error("Message is incomplete.");
  35. }
  36. // content before separator is handshake response - fy:分隔符前的内容是握手响应
  37. // optional content after is additional messages - fy:后面是附加消息的可选内容
  38. var responseLength = separatorIndex + 1;
  39. messageData = String.fromCharCode.apply(null, binaryData.slice(0, responseLength));
  40. remainingData = (binaryData.byteLength > responseLength) ? binaryData.slice(responseLength).buffer : null;
  41. }
  42. else {
  43. var textData = data;
  44. var separatorIndex = textData.indexOf(TextMessageFormat.RecordSeparator);
  45. if (separatorIndex === -1) {
  46. throw new Error("Message is incomplete.");
  47. }
  48. // content before separator is handshake response - fy:分隔符前的内容是握手响应
  49. // optional content after is additional messages - fy:后面是附加消息的可选内容
  50. var responseLength = separatorIndex + 1;
  51. messageData = textData.substring(0, responseLength);
  52. remainingData = (textData.length > responseLength) ? textData.substring(responseLength) : null;
  53. }
  54. // At this point we should have just the single handshake message - fy: 在这一点上,我们应该只有一个握手信息
  55. var messages = TextMessageFormat.parse(messageData);
  56. var response = JSON.parse(messages[0]);
  57. if (response.type) {
  58. throw new Error("Expected a handshake response from the server. -(fy: 需要来自服务器的握手响应)");
  59. }
  60. responseMessage = response;
  61. // multiple messages could have arrived with handshake - fy: 握手时可能会收到多条消息
  62. // return additional data to be parsed as usual, or null if all parsed - fy: 返回要像往常一样分析的其他数据,如果所有数据都已分析,则返回null
  63. return [remainingData, responseMessage];
  64. };
  65. return HandshakeProtocol;
  66. }());
  67. export { HandshakeProtocol };