uniapp,h5

polling.js 4.5KB

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