Skip to content

Commit

Permalink
fix(webtransport): properly handle abruptly closed connections
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jun 27, 2023
1 parent 01804d5 commit cf6aa1f
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions lib/transports/webtransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ export class WT extends Transport {
this.opts.transportOptions[this.name]
);

this.transport.closed.then(() => this.onClose());
this.transport.closed
.then(() => {
debug("transport closed gracefully");
this.onClose();
})
.catch((err) => {
debug("transport closed due to %s", err);
this.onError("webtransport error", err);
});

// note: we could have used async/await, but that would require some additional polyfills
this.transport.ready.then(() => {
Expand All @@ -50,23 +58,28 @@ export class WT extends Transport {
let binaryFlag;

const read = () => {
reader.read().then(({ done, value }) => {
if (done) {
debug("session is closed");
return;
}
debug("received chunk: %o", value);
if (!binaryFlag && value.byteLength === 1 && value[0] === 54) {
binaryFlag = true;
} else {
// TODO expose binarytype
this.onPacket(
decodePacketFromBinary(value, binaryFlag, "arraybuffer")
);
binaryFlag = false;
}
read();
});
reader
.read()
.then(({ done, value }) => {
if (done) {
debug("session is closed");
return;
}
debug("received chunk: %o", value);
if (!binaryFlag && value.byteLength === 1 && value[0] === 54) {
binaryFlag = true;
} else {
// TODO expose binarytype
this.onPacket(
decodePacketFromBinary(value, binaryFlag, "arraybuffer")
);
binaryFlag = false;
}
read();
})
.catch((err) => {
debug("an error occurred while reading: %s", err);
});
};

read();
Expand Down

0 comments on commit cf6aa1f

Please sign in to comment.