uniapp,h5

websocket.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.WS = exports.BaseWS = 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 globals_node_js_1 = require("../globals.node.js");
  11. const debug_1 = __importDefault(require("debug")); // debug()
  12. const debug = (0, debug_1.default)("engine.io-client:websocket"); // debug()
  13. // detect ReactNative environment
  14. const isReactNative = typeof navigator !== "undefined" &&
  15. typeof navigator.product === "string" &&
  16. navigator.product.toLowerCase() === "reactnative";
  17. class BaseWS extends transport_js_1.Transport {
  18. get name() {
  19. return "websocket";
  20. }
  21. doOpen() {
  22. const uri = this.uri();
  23. const protocols = this.opts.protocols;
  24. // React Native only supports the 'headers' option, and will print a warning if anything else is passed
  25. const opts = isReactNative
  26. ? {}
  27. : (0, util_js_1.pick)(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");
  28. if (this.opts.extraHeaders) {
  29. opts.headers = this.opts.extraHeaders;
  30. }
  31. try {
  32. this.ws = this.createSocket(uri, protocols, opts);
  33. }
  34. catch (err) {
  35. return this.emitReserved("error", err);
  36. }
  37. this.ws.binaryType = this.socket.binaryType;
  38. this.addEventListeners();
  39. }
  40. /**
  41. * Adds event listeners to the socket
  42. *
  43. * @private
  44. */
  45. addEventListeners() {
  46. this.ws.onopen = () => {
  47. if (this.opts.autoUnref) {
  48. this.ws._socket.unref();
  49. }
  50. this.onOpen();
  51. };
  52. this.ws.onclose = (closeEvent) => this.onClose({
  53. description: "websocket connection closed",
  54. context: closeEvent,
  55. });
  56. this.ws.onmessage = (ev) => this.onData(ev.data);
  57. this.ws.onerror = (e) => this.onError("websocket error", e);
  58. }
  59. write(packets) {
  60. this.writable = false;
  61. // encodePacket efficient as it uses WS framing
  62. // no need for encodePayload
  63. for (let i = 0; i < packets.length; i++) {
  64. const packet = packets[i];
  65. const lastPacket = i === packets.length - 1;
  66. (0, engine_io_parser_1.encodePacket)(packet, this.supportsBinary, (data) => {
  67. // Sometimes the websocket has already been closed but the browser didn't
  68. // have a chance of informing us about it yet, in that case send will
  69. // throw an error
  70. try {
  71. this.doWrite(packet, data);
  72. }
  73. catch (e) {
  74. debug("websocket closed before onclose event");
  75. }
  76. if (lastPacket) {
  77. // fake drain
  78. // defer to next tick to allow Socket to clear writeBuffer
  79. (0, globals_node_js_1.nextTick)(() => {
  80. this.writable = true;
  81. this.emitReserved("drain");
  82. }, this.setTimeoutFn);
  83. }
  84. });
  85. }
  86. }
  87. doClose() {
  88. if (typeof this.ws !== "undefined") {
  89. this.ws.close();
  90. this.ws = null;
  91. }
  92. }
  93. /**
  94. * Generates uri for connection.
  95. *
  96. * @private
  97. */
  98. uri() {
  99. const schema = this.opts.secure ? "wss" : "ws";
  100. const query = this.query || {};
  101. // append timestamp to URI
  102. if (this.opts.timestampRequests) {
  103. query[this.opts.timestampParam] = (0, util_js_1.randomString)();
  104. }
  105. // communicate binary support capabilities
  106. if (!this.supportsBinary) {
  107. query.b64 = 1;
  108. }
  109. return this.createUri(schema, query);
  110. }
  111. }
  112. exports.BaseWS = BaseWS;
  113. const WebSocketCtor = globals_node_js_1.globalThisShim.WebSocket || globals_node_js_1.globalThisShim.MozWebSocket;
  114. /**
  115. * WebSocket transport based on the built-in `WebSocket` object.
  116. *
  117. * Usage: browser, Node.js (since v21), Deno, Bun
  118. *
  119. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
  120. * @see https://caniuse.com/mdn-api_websocket
  121. * @see https://nodejs.org/api/globals.html#websocket
  122. */
  123. class WS extends BaseWS {
  124. createSocket(uri, protocols, opts) {
  125. return !isReactNative
  126. ? protocols
  127. ? new WebSocketCtor(uri, protocols)
  128. : new WebSocketCtor(uri)
  129. : new WebSocketCtor(uri, protocols, opts);
  130. }
  131. doWrite(_packet, data) {
  132. this.ws.send(data);
  133. }
  134. }
  135. exports.WS = WS;