uniapp,h5

websocket.node.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.WS = void 0;
  4. const ws_1 = require("ws");
  5. const websocket_js_1 = require("./websocket.js");
  6. /**
  7. * WebSocket transport based on the `WebSocket` object provided by the `ws` package.
  8. *
  9. * Usage: Node.js, Deno (compat), Bun (compat)
  10. *
  11. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
  12. * @see https://caniuse.com/mdn-api_websocket
  13. */
  14. class WS extends websocket_js_1.BaseWS {
  15. createSocket(uri, protocols, opts) {
  16. var _a;
  17. if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a._cookieJar) {
  18. opts.headers = opts.headers || {};
  19. opts.headers.cookie =
  20. typeof opts.headers.cookie === "string"
  21. ? [opts.headers.cookie]
  22. : opts.headers.cookie || [];
  23. for (const [name, cookie] of this.socket._cookieJar.cookies) {
  24. opts.headers.cookie.push(`${name}=${cookie.value}`);
  25. }
  26. }
  27. return new ws_1.WebSocket(uri, protocols, opts);
  28. }
  29. doWrite(packet, data) {
  30. const opts = {};
  31. if (packet.options) {
  32. opts.compress = packet.options.compress;
  33. }
  34. if (this.opts.perMessageDeflate) {
  35. const len =
  36. // @ts-ignore
  37. "string" === typeof data ? Buffer.byteLength(data) : data.length;
  38. if (len < this.opts.perMessageDeflate.threshold) {
  39. opts.compress = false;
  40. }
  41. }
  42. this.ws.send(data, opts);
  43. }
  44. }
  45. exports.WS = WS;