Skip to content

Commit 6ca38d3

Browse files
committed
feat(DataConnection): handle close messages and flush option
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
1 parent 963455e commit 6ca38d3

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

lib/dataconnection.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,15 @@ export class DataConnection
179179
deserializedData = this.parse(data as string);
180180
}
181181

182-
// Check if we've chunked--if so, piece things back together.
183-
// We're guaranteed that this isn't 0.
184-
if (deserializedData.__peerData) {
182+
// PeerJS specific message
183+
const peerData = deserializedData["__peerData"];
184+
if (peerData) {
185+
if (peerData.type === "close") {
186+
this.close();
187+
return;
188+
}
189+
190+
// Chunked data -- piece things back together.
185191
this._handleChunk(deserializedData);
186192
return;
187193
}
@@ -221,7 +227,15 @@ export class DataConnection
221227
*/
222228

223229
/** Allows user to close connection. */
224-
close(): void {
230+
close(options?: { flush?: boolean }) {
231+
if (options?.flush) {
232+
this.send({
233+
__peerData: {
234+
type: "close",
235+
},
236+
});
237+
return;
238+
}
225239
this._buffer = [];
226240
this._bufferSize = 0;
227241
this._chunkedData = {};

0 commit comments

Comments
 (0)