playsocketjs 4.3.1 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -305,7 +305,7 @@ The callback signature is `callback(verified, code?, message?)` where `code` ref
305
305
  #### Methods
306
306
 
307
307
  > [!IMPORTANT]
308
- > Rooms created by the server default to host ID "server". If that host string is used, the room will not be auto-deleted when all participants have left and you need to take care of its lifecycle.
308
+ > Rooms created by the server are always owned by the host ID "server". Such rooms are not auto-deleted when all participants have left, so you need to take care of their lifecycle.
309
309
 
310
310
  | Name | Parameters | Return type | Description |
311
311
  |--------|------------|-------------|-------------|
@@ -316,7 +316,7 @@ The callback signature is `callback(verified, code?, message?)` where `code` ref
316
316
  | `getRoomStorage()` | `roomId: string` | `object` | Get a snapshot of the current room storage. |
317
317
  | `getUpdateDetails()` | `update: object` | `object` | Get the details (`key`, `type`, `value` and `secondValue`) of a storage update for building validation logic. |
318
318
  | `updateRoomStorage()` | `roomId: string, key: string, type: string, value: any, secondValue?: any` | `void` | Update a key in the shared storage of a room. |
319
- | `createRoom()` | `initialStorage?: object, size?: number, host?: string` | `object` | Create a room (returns object containing room ID and state).|
319
+ | `createRoom()` | `initialStorage?: object, size?: number` | `object` | Create a server-owned room (returns object containing room ID and state).|
320
320
  | `destroyRoom()` | `roomId: string` | `void` | Destroy a room & kick all participants. |
321
321
 
322
322
  #### Events
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "4.3.1",
3
+ "version": "5.0.0",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "type": "module",
6
6
  "module": "./src/client/client.js",
@@ -152,7 +152,7 @@ export default class PlaySocketServer {
152
152
  }
153
153
 
154
154
  // Register client ID if provided & check for a duplicate
155
- if (data.id && (this.#clients.get(data.id) || data.id === "server")) {
155
+ if (data.id && (this.#clients.get(data.id) || this.#pendingDisconnects.has(data.id) || data.id === "server")) {
156
156
  ws.send(encode({ type: "registration_failed", reason: "ID is taken" }), { binary: true });
157
157
  return;
158
158
  }
@@ -161,7 +161,7 @@ export default class PlaySocketServer {
161
161
  if (!data.id) {
162
162
  for (let i = 0; i < 50; i++) {
163
163
  const id = this.#generateId();
164
- if (!this.#clients.get(id)) {
164
+ if (!this.#clients.get(id) && !this.#pendingDisconnects.has(id)) {
165
165
  data.id = id;
166
166
  break;
167
167
  }
@@ -258,7 +258,7 @@ export default class PlaySocketServer {
258
258
  return;
259
259
  }
260
260
 
261
- const newRoom = this.createRoom(data.initialStorage, data.size, ws.clientId); // Create room
261
+ const newRoom = this.#createRoom(ws.clientId, data.initialStorage, data.size); // Create room
262
262
 
263
263
  this.#rooms[newRoom.id].participants.push(ws.clientId) // Add client to the room
264
264
  this.#clientRooms.set(ws.clientId, newRoom.id); // Add client to the client-room map
@@ -604,14 +604,24 @@ export default class PlaySocketServer {
604
604
  }
605
605
  }
606
606
 
607
+ /**
608
+ * Create a server-owned room
609
+ * @param {object} [initialStorage] - Optional initial storage object
610
+ * @param {number} [size] - Max. room size, up to 500
611
+ * @returns {{ state: CRDTState, id: string }} Object containing room state and room ID
612
+ */
613
+ createRoom(initialStorage, size) {
614
+ return this.#createRoom("server", initialStorage, size);
615
+ }
616
+
607
617
  /**
608
618
  * Create a room
619
+ * @param {string} host - Host ID (when set to "server", room will not be deleted if all clients leave)
609
620
  * @param {object} [initialStorage] - Optional initial storage object
610
621
  * @param {number} [size] - Max. room size, up to 500
611
- * @param {string} [host] - Host ID, defaults to "server" (when set to "server", room will not be deleted if all clients leave)
612
622
  * @returns {{ state: CRDTState, id: string }} Object containing room state and room ID
613
623
  */
614
- createRoom(initialStorage, size, host = "server") {
624
+ #createRoom(host, initialStorage, size) {
615
625
  let newRoomId;
616
626
 
617
627
  for (let i = 0; i < 100; i++) {
@@ -9,7 +9,7 @@ export const HEARTBEAT_INTERVAL = 5000;
9
9
  /**
10
10
  * Library version, must match package.json
11
11
  */
12
- export const VERSION = "4.3.0";
12
+ export const VERSION = "5.0.0";
13
13
 
14
14
  /**
15
15
  * Logging prefixes
@@ -120,13 +120,12 @@ export default class PlaySocketServer {
120
120
  */
121
121
  updateRoomStorage(roomId: string, key: string, type: PropertyUpdateType, value: any, secondValue?: any): void;
122
122
  /**
123
- * Create a room
123
+ * Create a server-owned room
124
124
  * @param {object} [initialStorage] - Optional initial storage object
125
125
  * @param {number} [size] - Max. room size, up to 500
126
- * @param {string} [host] - Host ID, defaults to "server" (when set to "server", room will not be deleted if all clients leave)
127
126
  * @returns {{ state: CRDTState, id: string }} Object containing room state and room ID
128
127
  */
129
- createRoom(initialStorage?: object, size?: number, host?: string): {
128
+ createRoom(initialStorage?: object, size?: number): {
130
129
  state: CRDTState;
131
130
  id: string;
132
131
  };
@@ -8,7 +8,7 @@ export declare const HEARTBEAT_INTERVAL = 5000;
8
8
  /**
9
9
  * Library version, must match package.json
10
10
  */
11
- export declare const VERSION = "4.3.0";
11
+ export declare const VERSION = "5.0.0";
12
12
  /**
13
13
  * Logging prefixes
14
14
  */