uniapp,h5

polling.js 3.9KB

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