playsocketjs 3.0.0 → 3.1.1

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
@@ -257,9 +257,10 @@ The callback signature is `callback(verified, code?, message?)` where `code` and
257
257
  |-------|-------------------|-------------|--------------|
258
258
  | `clientRegistered` | `clientId: string, customData: object` | Client registered with the server | - |
259
259
  | `clientRegistrationRequested` | `clientId: string, customData: object` | Client requests to register | Return `false` or rejection reason `string` to block |
260
- | `clientLeft` | `clientId: string` | Client disconnected from the server | - |
261
- | `clientJoinedRoom` | `clientId: string, roomId: string` | Client joined a room (clients can only leave by disconnecting) | - |
260
+ | `clientDisconnected` | `clientId: string` | Client disconnected from the server | - |
261
+ | `clientJoinedRoom` | `clientId: string, roomId: string` | Client joined a room | - |
262
262
  | `clientJoinRequested` | `clientId: string, roomId: string` | Client requests to join a room | Return `false` or rejection reason `string` to block |
263
+ | `clientLeftRoom` | `clientId: string, roomId: string` | Client left a room | - |
263
264
  | `roomCreated` | `roomId: string` | Client created a room | - |
264
265
  | `roomDestroyed` | `roomId: string` | Room was destroyed (happens when all participants leave, unless room host is "server") | - |
265
266
  | `roomCreationRequested` | `{clientId: string, initialStorage: object}` | Room creation requested by client | Return `object` to override initial storage, `false` to deny |
@@ -2180,7 +2180,7 @@ class PlaySocket {
2180
2180
  */
2181
2181
  #createTimeout(name) {
2182
2182
  return new Promise((_, reject) =>
2183
- setTimeout(() => reject(new Error(`${name} timed out`)), TIMEOUT_MS)
2183
+ setTimeout(() => reject(new Error(`${name} timed out.`)), TIMEOUT_MS)
2184
2184
  );
2185
2185
  }
2186
2186
 
@@ -2220,8 +2220,8 @@ class PlaySocket {
2220
2220
  * @returns {Promise} Resolves when connection is established
2221
2221
  */
2222
2222
  async init() {
2223
- if (this.#initialized) return Promise.reject(new Error("Already initialized"));
2224
- if (!this.#endpoint) return Promise.reject(new Error("No websocket endpoint provided"));
2223
+ if (this.#initialized) return Promise.reject(new Error("Already initialized!"));
2224
+ if (!this.#endpoint) return Promise.reject(new Error("No websocket endpoint provided!"));
2225
2225
  this.#triggerEvent("status", "Initializing...");
2226
2226
 
2227
2227
  // Connect to WS server
@@ -2400,7 +2400,7 @@ class PlaySocket {
2400
2400
  // Handle socket errors
2401
2401
  this.#socket.onerror = () => {
2402
2402
  this.#triggerEvent("error", "WebSocket error.");
2403
- if (this.#pendingConnect) this.#pendingConnect.reject(new Error("WebSocket error"));
2403
+ if (this.#pendingConnect) this.#pendingConnect.reject(new Error("WebSocket error!"));
2404
2404
  };
2405
2405
 
2406
2406
  // Handle socket close & attempt reconnect
@@ -2467,7 +2467,7 @@ class PlaySocket {
2467
2467
  */
2468
2468
  #sendToServer(data) {
2469
2469
  if (!this.#socket || this.#socket?.readyState !== WebSocket.OPEN) {
2470
- console.warn(WARNING_PREFIX + "Cannot send message - not connected.");
2470
+ console.warn(WARNING_PREFIX + "Failed to send message - not connected.");
2471
2471
  return;
2472
2472
  }
2473
2473
  try {
@@ -2486,8 +2486,8 @@ class PlaySocket {
2486
2486
  */
2487
2487
  async createRoom(initialStorage = {}, size) {
2488
2488
  if (!this.#initialized) {
2489
- this.#triggerEvent("error", "Cannot create room - not initialized");
2490
- return Promise.reject(new Error("Not initialized"));
2489
+ this.#triggerEvent("error", "Failed to create room - not initialized");
2490
+ return Promise.reject(new Error("Not initialized!"));
2491
2491
  }
2492
2492
 
2493
2493
  return Promise.race([
@@ -2513,8 +2513,8 @@ class PlaySocket {
2513
2513
  */
2514
2514
  async joinRoom(roomId) {
2515
2515
  if (!this.#initialized) {
2516
- this.#triggerEvent("error", "Cannot join room - not initialized");
2517
- return Promise.reject(new Error("Not initialized"));
2516
+ this.#triggerEvent("error", "Failed to join room - not initialized");
2517
+ return Promise.reject(new Error("Not initialized!"));
2518
2518
  }
2519
2519
 
2520
2520
  return Promise.race([
@@ -2543,7 +2543,7 @@ class PlaySocket {
2543
2543
  */
2544
2544
  updateStorage(key, type, value, updateValue) {
2545
2545
  if (!this.#inRoom) {
2546
- this.#triggerEvent("error", "Cannot update storage when not in a room.");
2546
+ this.#triggerEvent("error", "Failed to update storage - not in a room.");
2547
2547
  return;
2548
2548
  } if (this.#debug) console.log(LOG_PREFIX + `Property update for key ${key}, operation ${type}, value ${value} and updateValue ${updateValue}.`);
2549
2549
  const propUpdate = this.#crdtManager.updateProperty(key, type, value, updateValue);
@@ -2334,7 +2334,7 @@ class PlaySocketServer {
2334
2334
 
2335
2335
  const newRoom = this.createRoom(data.initialStorage, data.size, ws.clientId); // Create room
2336
2336
 
2337
- // Check if client/creator is still connected - abort if not
2337
+ // Check if client/creator is still connected and abort if not
2338
2338
  if (!this.#clients.has(ws.clientId)) {
2339
2339
  this.destroyRoom(newRoom.id);
2340
2340
  if (this.#debug) console.log(`Room creation cancelled - client ${ws.clientId} disconnected during event callback.`);
@@ -2538,7 +2538,7 @@ class PlaySocketServer {
2538
2538
  this.#clientTokens.delete(ws.clientId);
2539
2539
  const roomId = this.#clientRooms.get(ws.clientId);
2540
2540
  if (roomId && this.#rooms[roomId]) this.#leaveRoom(ws.clientId, roomId);
2541
- this.#triggerEvent("clientDisconnected", ws.clientId, roomId);
2541
+ this.#triggerEvent("clientDisconnected", ws.clientId);
2542
2542
  }
2543
2543
 
2544
2544
  /**
@@ -2547,7 +2547,7 @@ class PlaySocketServer {
2547
2547
  * @param {Function} callback - Callback function
2548
2548
  */
2549
2549
  onEvent(event, callback) {
2550
- const validEvents = ["clientRegistered", "clientRegistrationRequested", "clientDisconnected", "clientJoinedRoom", "clientJoinRequested", "roomCreated", "roomCreationRequested", "requestReceived", "storageUpdated", "storageUpdateRequested", "roomDestroyed"];
2550
+ const validEvents = ["clientRegistered", "clientRegistrationRequested", "clientDisconnected", "clientJoinedRoom", "clientLeftRoom", "clientJoinRequested", "roomCreated", "roomCreationRequested", "requestReceived", "storageUpdated", "storageUpdateRequested", "roomDestroyed"];
2551
2551
  if (!validEvents.includes(event)) {
2552
2552
  console.warn(`Invalid PlaySocket event type "${event}"`);
2553
2553
  return;
@@ -2748,6 +2748,7 @@ class PlaySocketServer {
2748
2748
 
2749
2749
  room.participants = room.participants.filter(p => p !== clientId); // Remove client from room
2750
2750
  this.#clientRooms.delete(clientId);
2751
+ this.#triggerEvent("clientLeftRoom", clientId, roomId); // Before potential room destruction to allow for accessing its data in callback
2751
2752
 
2752
2753
  if (room.participants.length === 0 && room.host !== "server") {
2753
2754
  this.destroyRoom(roomId); // Destroy room if now empty & not by server
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "type": "module",
6
6
  "module": "dist/playsocket-client.js",