uniapp,h5

polling.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Polling = void 0;
  7. const transport_js_1 = require("../transport.js");
  8. const util_js_1 = require("../util.js");
  9. const engine_io_parser_1 = require("engine.io-parser");
  10. const debug_1 = __importDefault(require("debug")); // debug()
  11. const debug = (0, debug_1.default)("engine.io-client:polling"); // debug()
  12. class Polling extends transport_js_1.Transport {
  13. constructor() {
  14. super(...arguments);
  15. this._polling = false;
  16. }
  17. get name() {
  18. return "polling";
  19. }
  20. /**
  21. * Opens the socket (triggers polling). We write a PING message to determine
  22. * when the transport is open.
  23. *
  24. * @protected
  25. */
  26. doOpen() {
  27. this._poll();
  28. }
  29. /**
  30. * Pauses polling.
  31. *
  32. * @param {Function} onPause - callback upon buffers are flushed and transport is paused
  33. * @package
  34. */
  35. pause(onPause) {
  36. this.readyState = "pausing";
  37. const pause = () => {
  38. debug("paused");
  39. this.readyState = "paused";
  40. onPause();
  41. };
  42. if (this._polling || !this.writable) {
  43. let total = 0;
  44. if (this._polling) {
  45. debug("we are currently polling - waiting to pause");
  46. total++;
  47. this.once("pollComplete", function () {
  48. debug("pre-pause polling complete");
  49. --total || pause();
  50. });
  51. }
  52. if (!this.writable) {
  53. debug("we are currently writing - waiting to pause");
  54. total++;
  55. this.once("drain", function () {
  56. debug("pre-pause writing complete");
  57. --total || pause();
  58. });
  59. }
  60. }
  61. else {
  62. pause();
  63. }
  64. }
  65. /**
  66. * Starts polling cycle.
  67. *
  68. * @private
  69. */
  70. _poll() {
  71. debug("polling");
  72. this._polling = true;
  73. this.doPoll();
  74. this.emitReserved("poll");
  75. }
  76. /**
  77. * Overloads onData to detect payloads.
  78. *
  79. * @protected
  80. */
  81. onData(data) {
  82. debug("polling got data %s", data);
  83. const callback = (packet) => {
  84. // if its the first message we consider the transport open
  85. if ("opening" === this.readyState && packet.type === "open") {
  86. this.onOpen();
  87. }
  88. // if its a close packet, we close the ongoing requests
  89. if ("close" === packet.type) {
  90. this.onClose({ description: "transport closed by the server" });
  91. return false;
  92. }
  93. // otherwise bypass onData and handle the message
  94. this.onPacket(packet);
  95. };
  96. // decode payload
  97. (0, engine_io_parser_1.decodePayload)(data, this.socket.binaryType).forEach(callback);
  98. // if an event did not trigger closing
  99. if ("closed" !== this.readyState) {
  100. // if we got data we're not polling
  101. this._polling = false;
  102. this.emitReserved("pollComplete");
  103. if ("open" === this.readyState) {
  104. this._poll();
  105. }
  106. else {
  107. debug('ignoring poll - transport state "%s"', this.readyState);
  108. }
  109. }
  110. }
  111. /**
  112. * For polling, send a close packet.
  113. *
  114. * @protected
  115. */
  116. doClose() {
  117. const close = () => {
  118. debug("writing close packet");
  119. this.write([{ type: "close" }]);
  120. };
  121. if ("open" === this.readyState) {
  122. debug("transport open - closing");
  123. close();
  124. }
  125. else {
  126. // in case we're trying to close while
  127. // handshaking is in progress (GH-164)
  128. debug("transport not open - deferring close");
  129. this.once("open", close);
  130. }
  131. }
  132. /**
  133. * Writes a packets payload.
  134. *
  135. * @param {Array} packets - data packets
  136. * @protected
  137. */
  138. write(packets) {
  139. this.writable = false;
  140. (0, engine_io_parser_1.encodePayload)(packets, (data) => {
  141. this.doWrite(data, () => {
  142. this.writable = true;
  143. this.emitReserved("drain");
  144. });
  145. });
  146. }
  147. /**
  148. * Generates uri for connection.
  149. *
  150. * @private
  151. */
  152. uri() {
  153. const schema = this.opts.secure ? "https" : "http";
  154. const query = this.query || {};
  155. // cache busting is forced
  156. if (false !== this.opts.timestampRequests) {
  157. query[this.opts.timestampParam] = (0, util_js_1.randomString)();
  158. }
  159. if (!this.supportsBinary && !query.sid) {
  160. query.b64 = 1;
  161. }
  162. return this.createUri(schema, query);
  163. }
  164. }
  165. exports.Polling = Polling;