Skip to content

Commit

Permalink
feat(DataConnection): handle close messages and flush option
Browse files Browse the repository at this point in the history
This commit enhances the DataConnection class to manage PeerJS specific `close` messages. It closes the connection when the message type is "close" and sends a close message if `options?.flush` is set, ensuring the buffer is flushed before closing.

Closes #982
  • Loading branch information
jonasgloning committed Jun 21, 2023
1 parent 963455e commit 6ca38d3
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/dataconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,15 @@ export class DataConnection
deserializedData = this.parse(data as string);
}

// Check if we've chunked--if so, piece things back together.
// We're guaranteed that this isn't 0.
if (deserializedData.__peerData) {
// PeerJS specific message
const peerData = deserializedData["__peerData"];
if (peerData) {
if (peerData.type === "close") {
this.close();
return;
}

// Chunked data -- piece things back together.
this._handleChunk(deserializedData);
return;
}
Expand Down Expand Up @@ -221,7 +227,15 @@ export class DataConnection
*/

/** Allows user to close connection. */
close(): void {
close(options?: { flush?: boolean }) {
if (options?.flush) {
this.send({
__peerData: {
type: "close",
},
});
return;
}
this._buffer = [];
this._bufferSize = 0;
this._chunkedData = {};
Expand Down

0 comments on commit 6ca38d3

Please sign in to comment.