websocket.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.WebSocket = void 0;
  4. const transport_1 = require("../transport");
  5. const debug_1 = require("debug");
  6. const debug = (0, debug_1.default)("engine:ws");
  7. class WebSocket extends transport_1.Transport {
  8. /**
  9. * WebSocket transport
  10. *
  11. * @param {EngineRequest} req
  12. */
  13. constructor(req) {
  14. super(req);
  15. this._doSend = (data) => {
  16. this.socket.send(data, this._onSent);
  17. };
  18. this._doSendLast = (data) => {
  19. this.socket.send(data, this._onSentLast);
  20. };
  21. this._onSent = (err) => {
  22. if (err) {
  23. this.onError("write error", err.stack);
  24. }
  25. };
  26. this._onSentLast = (err) => {
  27. if (err) {
  28. this.onError("write error", err.stack);
  29. }
  30. else {
  31. this.emit("drain");
  32. this.writable = true;
  33. this.emit("ready");
  34. }
  35. };
  36. this.socket = req.websocket;
  37. this.socket.on("message", (data, isBinary) => {
  38. const message = isBinary ? data : data.toString();
  39. debug('received "%s"', message);
  40. super.onData(message);
  41. });
  42. this.socket.once("close", this.onClose.bind(this));
  43. this.socket.on("error", this.onError.bind(this));
  44. this.writable = true;
  45. this.perMessageDeflate = null;
  46. }
  47. /**
  48. * Transport name
  49. */
  50. get name() {
  51. return "websocket";
  52. }
  53. /**
  54. * Advertise upgrade support.
  55. */
  56. get handlesUpgrades() {
  57. return true;
  58. }
  59. send(packets) {
  60. this.writable = false;
  61. for (let i = 0; i < packets.length; i++) {
  62. const packet = packets[i];
  63. const isLast = i + 1 === packets.length;
  64. if (this._canSendPreEncodedFrame(packet)) {
  65. // the WebSocket frame was computed with WebSocket.Sender.frame()
  66. // see https://github.com/websockets/ws/issues/617#issuecomment-283002469
  67. this.socket._sender.sendFrame(
  68. // @ts-ignore
  69. packet.options.wsPreEncodedFrame, isLast ? this._onSentLast : this._onSent);
  70. }
  71. else {
  72. this.parser.encodePacket(packet, this.supportsBinary, isLast ? this._doSendLast : this._doSend);
  73. }
  74. }
  75. }
  76. /**
  77. * Whether the encoding of the WebSocket frame can be skipped.
  78. * @param packet
  79. * @private
  80. */
  81. _canSendPreEncodedFrame(packet) {
  82. var _a, _b, _c;
  83. return (!this.perMessageDeflate &&
  84. typeof ((_b = (_a = this.socket) === null || _a === void 0 ? void 0 : _a._sender) === null || _b === void 0 ? void 0 : _b.sendFrame) === "function" &&
  85. // @ts-ignore
  86. ((_c = packet.options) === null || _c === void 0 ? void 0 : _c.wsPreEncodedFrame) !== undefined);
  87. }
  88. doClose(fn) {
  89. debug("closing");
  90. this.socket.close();
  91. fn && fn();
  92. }
  93. }
  94. exports.WebSocket = WebSocket;