uniapp,h5

transport.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { decodePacket } from "engine.io-parser";
  2. import { Emitter } from "@socket.io/component-emitter";
  3. import { installTimerFunctions } from "./util.js";
  4. import { encode } from "./contrib/parseqs.js";
  5. export class TransportError extends Error {
  6. constructor(reason, description, context) {
  7. super(reason);
  8. this.description = description;
  9. this.context = context;
  10. this.type = "TransportError";
  11. }
  12. }
  13. export class Transport extends Emitter {
  14. /**
  15. * Transport abstract constructor.
  16. *
  17. * @param {Object} opts - options
  18. * @protected
  19. */
  20. constructor(opts) {
  21. super();
  22. this.writable = false;
  23. installTimerFunctions(this, opts);
  24. this.opts = opts;
  25. this.query = opts.query;
  26. this.socket = opts.socket;
  27. this.supportsBinary = !opts.forceBase64;
  28. }
  29. /**
  30. * Emits an error.
  31. *
  32. * @param {String} reason
  33. * @param description
  34. * @param context - the error context
  35. * @return {Transport} for chaining
  36. * @protected
  37. */
  38. onError(reason, description, context) {
  39. super.emitReserved("error", new TransportError(reason, description, context));
  40. return this;
  41. }
  42. /**
  43. * Opens the transport.
  44. */
  45. open() {
  46. this.readyState = "opening";
  47. this.doOpen();
  48. return this;
  49. }
  50. /**
  51. * Closes the transport.
  52. */
  53. close() {
  54. if (this.readyState === "opening" || this.readyState === "open") {
  55. this.doClose();
  56. this.onClose();
  57. }
  58. return this;
  59. }
  60. /**
  61. * Sends multiple packets.
  62. *
  63. * @param {Array} packets
  64. */
  65. send(packets) {
  66. if (this.readyState === "open") {
  67. this.write(packets);
  68. }
  69. else {
  70. // this might happen if the transport was silently closed in the beforeunload event handler
  71. }
  72. }
  73. /**
  74. * Called upon open
  75. *
  76. * @protected
  77. */
  78. onOpen() {
  79. this.readyState = "open";
  80. this.writable = true;
  81. super.emitReserved("open");
  82. }
  83. /**
  84. * Called with data.
  85. *
  86. * @param {String} data
  87. * @protected
  88. */
  89. onData(data) {
  90. const packet = decodePacket(data, this.socket.binaryType);
  91. this.onPacket(packet);
  92. }
  93. /**
  94. * Called with a decoded packet.
  95. *
  96. * @protected
  97. */
  98. onPacket(packet) {
  99. super.emitReserved("packet", packet);
  100. }
  101. /**
  102. * Called upon close.
  103. *
  104. * @protected
  105. */
  106. onClose(details) {
  107. this.readyState = "closed";
  108. super.emitReserved("close", details);
  109. }
  110. /**
  111. * Pauses the transport, in order not to lose packets during an upgrade.
  112. *
  113. * @param onPause
  114. */
  115. pause(onPause) { }
  116. createUri(schema, query = {}) {
  117. return (schema +
  118. "://" +
  119. this._hostname() +
  120. this._port() +
  121. this.opts.path +
  122. this._query(query));
  123. }
  124. _hostname() {
  125. const hostname = this.opts.hostname;
  126. return hostname.indexOf(":") === -1 ? hostname : "[" + hostname + "]";
  127. }
  128. _port() {
  129. if (this.opts.port &&
  130. ((this.opts.secure && Number(this.opts.port !== 443)) ||
  131. (!this.opts.secure && Number(this.opts.port) !== 80))) {
  132. return ":" + this.opts.port;
  133. }
  134. else {
  135. return "";
  136. }
  137. }
  138. _query(query) {
  139. const encodedQuery = encode(query);
  140. return encodedQuery.length ? "?" + encodedQuery : "";
  141. }
  142. }