From 994be1f44b8cd589fa2458435e94956aa97a108f Mon Sep 17 00:00:00 2001 From: PaulPlay <80539587+therealPaulPlay@users.noreply.github.com> Date: Thu, 30 Jan 2025 18:59:34 +0100 Subject: [PATCH] Feat: New maxSize property for rooms --- README.md | 2 +- package.json | 2 +- src/index.js | 22 +++++++++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a3dbc4d..edb4a20 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Creates a new PlayPeer instance with a specified peer ID and [PeerJS options](ht #### Core - `init()`: Initialize the peer connection (async) -- `createRoom(initialStorage?: object)`: Create a new room and become host (async) +- `createRoom(initialStorage?: object, maxSize?: number)`: Create a new room and become host (async) - `joinRoom(hostId: string)`: Join an existing room. Returns promise, rejects after 2s timeout (async) - `destroy()`: Use this to leave a room. Destroys the peer instance diff --git a/package.json b/package.json index 2a5f8aa..c8d47cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "playpeerjs", - "version": "1.2.0", + "version": "1.2.1", "description": "WebRTC-based wrapper for creating robust peer-2-peer multiplayer systems with ease.", "type": "module", "main": "dist/playpeer.js", diff --git a/src/index.js b/src/index.js index aea9bcf..6f81fd7 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ export default class PlayPeer { #peer; #options; #initialized = false; + #maxSize; // Event callbacks stored in a map #callbacks = new Map(); @@ -115,7 +116,7 @@ export default class PlayPeer { this.#triggerEvent("error", "Connection attempt to singalling server timed out."); this.destroy(); reject(new Error("Connection attempt to signalling server timed out.")); - }, 2000); + }, 2 * 1000); this.#peer.on('open', () => { this.#triggerEvent("status", "Connected to signalling server!"); @@ -167,7 +168,15 @@ export default class PlayPeer { * @private */ #handleIncomingConnections(incomingConnection) { - // Close broken connections that don't open or address the wrong host (delay ensures the close event triggers on peers) + // Check if room is full + if (this.#isHost && this.#maxSize && (this.#hostConnections?.size + 1) >= this.#maxSize) { + console.warn(WARNING_PREFIX + `Connection ${incomingConnection.peer} rejected - room is full.`); + this.#triggerEvent("status", "Rejected connection - room is full."); + incomingConnection.close(); + return; // Don't continue with the rest of events + } + + // Close broken connections that don't open in time or address the wrong host setTimeout(() => { if (!incomingConnection.open || !this.#isHost) { try { @@ -254,9 +263,10 @@ export default class PlayPeer { /** * Create room and become host * @param {object} initialStorage - Initial storage object + * @param {number} [maxSize] - Optional maximum number of peers allowed in the room * @returns {Promise} Promise resolves with peer id */ - createRoom(initialStorage = {}) { + createRoom(initialStorage = {}, maxSize) { return new Promise((resolve, reject) => { if (!this.#peer || this.#peer.destroyed || !this.#initialized) { this.#triggerEvent("error", "Cannot create room if peer is not initialized. Note that .init() is async."); @@ -266,8 +276,9 @@ export default class PlayPeer { this.#isHost = true; this.#storage = initialStorage; + this.#maxSize = maxSize; // Store the maxSize value this.#triggerEvent("storageUpdate", { ...this.#storage }); - this.#triggerEvent("status", "Room created."); + this.#triggerEvent("status", `Room created${maxSize ? ` with size ${maxSize}` : ''}.`); resolve(this.id); }); } @@ -304,7 +315,7 @@ export default class PlayPeer { console.error(ERROR_PREFIX + "Connection attempt for joining room timed out."); this.#triggerEvent("status", "Connection attempt for joining room timed out."); reject(new Error("Connection attempt for joining room timed out.")); - }, 5 * 1000); + }, 3 * 1000); this.#outgoingConnection.on("open", () => { clearTimeout(timeout); @@ -587,6 +598,7 @@ export default class PlayPeer { this.#hostConnections.clear(); this.#hostConnectionsIdArray = []; this.#initialized = false; + this.#maxSize = undefined; // Clear intervals clearInterval(this.#heartbeatSendInterval);