()
| 26 | } |
| 27 | |
| 28 | protected doOpen() { |
| 29 | try { |
| 30 | // @ts-ignore |
| 31 | this._transport = new WebTransport( |
| 32 | this.createUri("https"), |
| 33 | this.opts.transportOptions[this.name], |
| 34 | ); |
| 35 | } catch (err) { |
| 36 | return this.emitReserved("error", err); |
| 37 | } |
| 38 | |
| 39 | this._transport.closed |
| 40 | .then(() => { |
| 41 | debug("transport closed gracefully"); |
| 42 | this.onClose(); |
| 43 | }) |
| 44 | .catch((err) => { |
| 45 | debug("transport closed due to %s", err); |
| 46 | this.onError("webtransport error", err); |
| 47 | }); |
| 48 | |
| 49 | // note: we could have used async/await, but that would require some additional polyfills |
| 50 | this._transport.ready.then(() => { |
| 51 | this._transport.createBidirectionalStream().then((stream) => { |
| 52 | const decoderStream = createPacketDecoderStream( |
| 53 | Number.MAX_SAFE_INTEGER, |
| 54 | this.socket.binaryType, |
| 55 | ); |
| 56 | const reader = stream.readable.pipeThrough(decoderStream).getReader(); |
| 57 | |
| 58 | const encoderStream = createPacketEncoderStream(); |
| 59 | encoderStream.readable.pipeTo(stream.writable); |
| 60 | this._writer = encoderStream.writable.getWriter(); |
| 61 | |
| 62 | const read = () => { |
| 63 | reader |
| 64 | .read() |
| 65 | .then(({ done, value }) => { |
| 66 | if (done) { |
| 67 | debug("session is closed"); |
| 68 | return; |
| 69 | } |
| 70 | debug("received chunk: %o", value); |
| 71 | this.onPacket(value); |
| 72 | read(); |
| 73 | }) |
| 74 | .catch((err) => { |
| 75 | debug("an error occurred while reading: %s", err); |
| 76 | }); |
| 77 | }; |
| 78 | |
| 79 | read(); |
| 80 | |
| 81 | const packet: Packet = { type: "open" }; |
| 82 | if (this.query.sid) { |
| 83 | packet.data = `{"sid":"${this.query.sid}"}`; |
| 84 | } |
| 85 | this._writer.write(packet).then(() => this.onOpen()); |
no test coverage detected