diff --git a/README.md b/README.md index edb4a20..df765e2 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,9 @@ Creates a new PlayPeer instance with a specified peer ID and [PeerJS options](ht - `status`: Connection status updates (returns status `string`) - `error`: Error events (returns error `string`) -- `destroy`: Peer destruction event +- `instanceDestroyed`: Peer destruction event - `storageUpdate`: Storage state changes (returns storage `object`) +- `hostMigration`: Host changes (returns host id / room code `string`) - `incomingPeerConnected`: New peer connected (returns peer-id `string`) - `incomingPeerDisconnected`: Peer disconnected (returns peer-id `string`) - `incomingPeerError`: Peer connection error (returns peer-id `string`) diff --git a/package-lock.json b/package-lock.json index a7ad3f1..3b92649 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "playpeerjs", - "version": "1.0.4", + "version": "1.2.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "playpeerjs", - "version": "1.0.4", + "version": "1.2.3", "license": "MIT", "devDependencies": { "@rollup/plugin-commonjs": "^28.0.2", diff --git a/package.json b/package.json index 7593f1d..ae393c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "playpeerjs", - "version": "1.2.3", + "version": "1.2.4", "description": "WebRTC-based wrapper for creating robust peer-2-peer multiplayer systems with ease.", "type": "module", "main": "dist/playpeer.js", diff --git a/src/example.html b/src/example.html index 8c60f99..d2b8368 100644 --- a/src/example.html +++ b/src/example.html @@ -48,13 +48,9 @@ let peer; - // Function to generate a random 10-character ID - function generateId() { - return Math.random().toString(36).substring(2, 12); - } - // Automatically generate and display the ID - const peerId = generateId(); + const peerId = Math.random().toString(36).substring(2, 12); + document.getElementById('peerIdDisplay').textContent = `Your peer ID: ${peerId}`; async function initPeer(id) { @@ -76,7 +72,6 @@ }); peer.onEvent('status', status => console.log('Status:', status)); - await peer.init(); } diff --git a/src/index.js b/src/index.js index ff4315a..13ae677 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,7 @@ const WARNING_PREFIX = "PlayPeer warning: "; */ export default class PlayPeer { // Config properties - id; + #id; #peer; #options; #initialized = false; @@ -36,7 +36,7 @@ export default class PlayPeer { * @param {object} [options] - Peer options (ice config, host, port etc.) */ constructor(id, options) { - this.id = id; + this.#id = id; if (options) this.#options = options; } @@ -49,8 +49,9 @@ export default class PlayPeer { const validEvents = [ "status", "error", - "destroy", + "instanceDestroyed", "storageUpdate", + "hostMigration", "incomingPeerConnected", "incomingPeerDisconnected", "incomingPeerError", @@ -92,12 +93,12 @@ export default class PlayPeer { async init() { return new Promise((resolve, reject) => { this.destroy(); // If peer already exists, destroy - if (!this.id) console.warn(ERROR_PREFIX + "No id provided!"); + if (!this.#id) console.warn(ERROR_PREFIX + "No id provided!"); if (!this.#options) console.warn(ERROR_PREFIX + "No config provided! Necessary stun and turn servers missing."); this.#triggerEvent("status", "Initializing instance..."); try { - this.#peer = new Peer(this.id, this.#options); + this.#peer = new Peer(this.#id, this.#options); } catch (error) { console.error(ERROR_PREFIX + "Failed to initialize peer:", error); this.#triggerEvent("error", "Failed to initialize peer: " + error); @@ -279,7 +280,7 @@ export default class PlayPeer { this.#maxSize = maxSize; // Store the maxSize value this.#triggerEvent("storageUpdate", { ...this.#storage }); this.#triggerEvent("status", `Room created${maxSize ? ` with size ${maxSize}` : ''}.`); - resolve(this.id); + resolve(this.#id); }); } @@ -556,15 +557,17 @@ export default class PlayPeer { const migrateToHostIndex = async (index) => { if (index >= connectedPeerIds.length) return; - if (connectedPeerIds[index] === this.id) { + if (connectedPeerIds[index] === this.#id) { this.#isHost = true; - this.#triggerEvent("status", `This peer (index ${index}) is now the host.`); this.#outgoingConnection = null; + this.#triggerEvent("status", `This peer (index ${index}) is now the host.`); + this.#triggerEvent("hostMigration", this.#id); } else { this.#triggerEvent("status", `Attempting to connect to new host (index ${index}) in 1s...`); try { - await new Promise(resolve => setTimeout(resolve, 1250)); + await new Promise(resolve => setTimeout(resolve, 1250)); // Wait to give new host time to detect disconnection & open room await this.joinRoom(connectedPeerIds[index]); + this.#triggerEvent("hostMigration", connectedPeerIds[index]); } catch (error) { this.#triggerEvent("error", "Error migrating host while connecting to new room: " + error); console.warn(WARNING_PREFIX + `Error migrating host (index ${index}) while connecting to new room:`, error); @@ -586,7 +589,7 @@ export default class PlayPeer { // Trigger events this.#triggerEvent("status", "Destroyed."); - this.#triggerEvent("destroy"); + this.#triggerEvent("instanceDestroyed"); } // Clear intervals @@ -628,4 +631,8 @@ export default class PlayPeer { get isHost() { return this.#isHost; } + + get id() { + return this.#id; + } } \ No newline at end of file