Skip to content

Commit

Permalink
Improve readme and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
therealPaulPlay committed Jan 23, 2025
1 parent 15085a2 commit 9fad06b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const hostId = await peer.createRoom({
await peer.joinRoom('host-peer-id'); // Rejects if connection fails or times out
const currentState = peer.getStorage;
peer.updateStorage('players', [...(currentState.players || []), newPlayer]);

// To leave the room, destroy the instance
peer.destroy();
```

## API Reference
Expand All @@ -69,10 +72,10 @@ Creates a new PlayPeer instance with a specified peer ID and [PeerJS options](ht

#### Core

- `init()`: Initialize the peer connection - returns promise
- `createRoom(initialStorage?: object)`: Create a new room and become host
- `joinRoom(hostId: string)`: Join an existing room. Returns promise, rejects after 2s timeout
- `destroy()`: Clean up and destroy the peer instance
- `init()`: Initialize the peer connection (async)
- `createRoom(initialStorage?: object)`: 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

#### State Management

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.0.7",
"version": "1.0.8",
"description": "WebRTC-based wrapper for creating robust peer-2-peer multiplayer systems with ease.",
"type": "module",
"main": "dist/playpeer.js",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class PlayPeer {
this.destroy(); // If peer already exists, destroy
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 new instance...");
this.#triggerEvent("status", "Initializing instance...");

try {
this.#peer = new Peer(this.id, this.#options);
Expand Down Expand Up @@ -245,7 +245,7 @@ export default class PlayPeer {
if (!this.#peer || this.#peer.destroyed) {
this.#triggerEvent("error", "Cannot create room if peer is not initialized.");
console.error(ERROR_PREFIX + "Cannot create room if peer is not initialized.");
reject(new Error("Failed to initialize peer."));
reject(new Error("Peer not initialized."));
}
return new Promise((resolve) => {
this.#isHost = true;
Expand All @@ -264,7 +264,7 @@ export default class PlayPeer {
if (!this.#peer || this.#peer.destroyed) {
this.#triggerEvent("error", "Cannot join room if peer is not initialized.");
console.error(ERROR_PREFIX + "Cannot join room if peer is not initialized.");
reject(new Error("Failed to initialize peer."));
reject(new Error("Peer not initialized."));
}
try {
if (this.#outgoingConnection) this.#outgoingConnection.close(); // Close previous connection (if exists)
Expand Down

0 comments on commit 9fad06b

Please sign in to comment.