Browse Source

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
Jonas Gloning 1 year ago
parent
commit
6ca38d32b0
1 changed files with 18 additions and 4 deletions
  1. 18 4
      lib/dataconnection.ts

+ 18 - 4
lib/dataconnection.ts

@@ -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;
 		}
@@ -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 = {};