* Writes a packet payload. * * @param {Array} packets * @private
(packets)
| 39 | * @private |
| 40 | */ |
| 41 | send(packets) { |
| 42 | this.writable = false; |
| 43 | |
| 44 | for (let i = 0; i < packets.length; i++) { |
| 45 | const packet = packets[i]; |
| 46 | const isLast = i + 1 === packets.length; |
| 47 | |
| 48 | const send = (data) => { |
| 49 | const isBinary = typeof data !== "string"; |
| 50 | const compress = |
| 51 | this.perMessageDeflate && |
| 52 | Buffer.byteLength(data) > this.perMessageDeflate.threshold; |
| 53 | |
| 54 | debug('writing "%s"', data); |
| 55 | this.socket.send(data, isBinary, compress); |
| 56 | |
| 57 | if (isLast) { |
| 58 | this.emit("drain"); |
| 59 | this.writable = true; |
| 60 | this.emit("ready"); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | if (packet.options && typeof packet.options.wsPreEncoded === "string") { |
| 65 | send(packet.options.wsPreEncoded); |
| 66 | } else { |
| 67 | this.parser.encodePacket(packet, this.supportsBinary, send); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Closes the transport. |