uniapp,h5

transport.js 3.7KB

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