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 {http.IncomingMessage}
|
---|
12 | * @api public
|
---|
13 | */
|
---|
14 | constructor(req) {
|
---|
15 | super(req);
|
---|
16 | this.socket = req.websocket;
|
---|
17 | this.socket.on("message", (data, isBinary) => {
|
---|
18 | const message = isBinary ? data : data.toString();
|
---|
19 | debug('received "%s"', message);
|
---|
20 | super.onData(message);
|
---|
21 | });
|
---|
22 | this.socket.once("close", this.onClose.bind(this));
|
---|
23 | this.socket.on("error", this.onError.bind(this));
|
---|
24 | this.writable = true;
|
---|
25 | this.perMessageDeflate = null;
|
---|
26 | }
|
---|
27 | /**
|
---|
28 | * Transport name
|
---|
29 | *
|
---|
30 | * @api public
|
---|
31 | */
|
---|
32 | get name() {
|
---|
33 | return "websocket";
|
---|
34 | }
|
---|
35 | /**
|
---|
36 | * Advertise upgrade support.
|
---|
37 | *
|
---|
38 | * @api public
|
---|
39 | */
|
---|
40 | get handlesUpgrades() {
|
---|
41 | return true;
|
---|
42 | }
|
---|
43 | /**
|
---|
44 | * Advertise framing support.
|
---|
45 | *
|
---|
46 | * @api public
|
---|
47 | */
|
---|
48 | get supportsFraming() {
|
---|
49 | return true;
|
---|
50 | }
|
---|
51 | /**
|
---|
52 | * Writes a packet payload.
|
---|
53 | *
|
---|
54 | * @param {Array} packets
|
---|
55 | * @api private
|
---|
56 | */
|
---|
57 | send(packets) {
|
---|
58 | const packet = packets.shift();
|
---|
59 | if (typeof packet === "undefined") {
|
---|
60 | this.writable = true;
|
---|
61 | this.emit("drain");
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | // always creates a new object since ws modifies it
|
---|
65 | const opts = {};
|
---|
66 | if (packet.options) {
|
---|
67 | opts.compress = packet.options.compress;
|
---|
68 | }
|
---|
69 | const send = data => {
|
---|
70 | if (this.perMessageDeflate) {
|
---|
71 | const len = "string" === typeof data ? Buffer.byteLength(data) : data.length;
|
---|
72 | if (len < this.perMessageDeflate.threshold) {
|
---|
73 | opts.compress = false;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | debug('writing "%s"', data);
|
---|
77 | this.writable = false;
|
---|
78 | this.socket.send(data, opts, err => {
|
---|
79 | if (err)
|
---|
80 | return this.onError("write error", err.stack);
|
---|
81 | this.send(packets);
|
---|
82 | });
|
---|
83 | };
|
---|
84 | if (packet.options && typeof packet.options.wsPreEncoded === "string") {
|
---|
85 | send(packet.options.wsPreEncoded);
|
---|
86 | }
|
---|
87 | else {
|
---|
88 | this.parser.encodePacket(packet, this.supportsBinary, send);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | /**
|
---|
92 | * Closes the transport.
|
---|
93 | *
|
---|
94 | * @api private
|
---|
95 | */
|
---|
96 | doClose(fn) {
|
---|
97 | debug("closing");
|
---|
98 | this.socket.close();
|
---|
99 | fn && fn();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | exports.WebSocket = WebSocket;
|
---|