Skip to content

Commit

Permalink
Feat: New maxSize property for rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
therealPaulPlay committed Jan 30, 2025
1 parent 4d4d021 commit 994be1f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class PlayPeer {
#peer;
#options;
#initialized = false;
#maxSize;

// Event callbacks stored in a map
#callbacks = new Map();
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.");
Expand All @@ -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);
});
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -587,6 +598,7 @@ export default class PlayPeer {
this.#hostConnections.clear();
this.#hostConnectionsIdArray = [];
this.#initialized = false;
this.#maxSize = undefined;

// Clear intervals
clearInterval(this.#heartbeatSendInterval);
Expand Down

0 comments on commit 994be1f

Please sign in to comment.