playsocketjs 1.1.13 → 1.2.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
@@ -63,10 +63,11 @@ socket.destroy();
63
63
  ### Constructor
64
64
 
65
65
  ```javascript
66
- new PlaySocket(id: string, options: PlaySocketOptions)
66
+ new PlaySocket(id?: string, options: PlaySocketOptions)
67
67
  ```
68
68
 
69
69
  Creates a new PlaySocket instance with a specified ID and configuration options.
70
+ Note: With PlaySocket, the id can be set to `null` to let the server pick a unique one.
70
71
 
71
72
  #### Configuration options
72
73
  - `endpoint`: WebSocket server endpoint (e.g., 'wss://example.com/socket')
@@ -77,7 +78,7 @@ Creates a new PlaySocket instance with a specified ID and configuration options.
77
78
 
78
79
  #### Core
79
80
 
80
- - `init()`: Initialize the WebSocket connection – Returns Promise (async)
81
+ - `init()`: Initialize the WebSocket connection – Returns Promise (async) which resolves with the client's id
81
82
  - `createRoom(initialStorage?: object, maxSize?: number)`: Create a new room and become host – Returns Promise (async) which resolves with the room id (matches the creator's id)
82
83
  - `joinRoom(hostId: string)`: Join an existing room – Returns Promise (async)
83
84
  - `destroy()`: Use this to leave a room and close the connection
@@ -2143,15 +2143,14 @@ class PlaySocket {
2143
2143
  async init() {
2144
2144
  if (this.#initialized) return Promise.reject(new Error("Already initialized"));
2145
2145
  if (!this.#endpoint) return Promise.reject(new Error("No websocket endpoint provided"));
2146
- if (!this.#id) return Promise.reject(new Error("No id provided"));
2147
2146
  this.#triggerEvent("status", "Initializing...");
2148
2147
 
2149
2148
  // Connect to WS server
2150
2149
  await this.#connect();
2151
2150
 
2152
2151
  // Register with server
2153
- await Promise.race([
2154
- new Promise(async (resolve, reject) => {
2152
+ const id = await Promise.race([
2153
+ new Promise((resolve, reject) => {
2155
2154
  this.#pendingRegistration = { resolve, reject }; // Resolve or reject depending on answer to registration msg
2156
2155
 
2157
2156
  // Register with server
@@ -2165,6 +2164,10 @@ class PlaySocket {
2165
2164
  ]).finally(() => {
2166
2165
  this.#pendingRegistration = null;
2167
2166
  });
2167
+
2168
+ // If the server sent out an id, store and return that
2169
+ if (id) this.#id = id;
2170
+ return this.#id;
2168
2171
  }
2169
2172
 
2170
2173
  /**
@@ -2273,7 +2276,7 @@ class PlaySocket {
2273
2276
  if (this.#pendingRegistration) {
2274
2277
  this.#sessionToken = message.sessionToken;
2275
2278
  this.#initialized = true;
2276
- this.#pendingRegistration.resolve();
2279
+ this.#pendingRegistration.resolve(message.id);
2277
2280
  this.#triggerEvent("status", "Connected to server.");
2278
2281
  }
2279
2282
  break;
@@ -2273,17 +2273,35 @@ function requireServer () {
2273
2273
 
2274
2274
  switch (data.type) {
2275
2275
  case 'register':
2276
- // Register client ID
2277
- if (!data.id) return;
2278
- if (this.#clients.get(data.id)) {
2276
+ // Register client ID if provided & check for a duplicate
2277
+ if (data.id && this.#clients.get(data.id)) {
2279
2278
  ws.send(encode({ type: 'id_taken' }), { binary: true });
2280
2279
  return;
2281
2280
  }
2281
+
2282
+ // Generate client ID if none provided
2283
+ if (!data.id) {
2284
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
2285
+ const maxAttempts = 50;
2286
+ for (let i = 0; i < maxAttempts; i++) {
2287
+ const id = Array.from({ length: 6 }, () => chars[Math.floor(Math.random() * chars.length)]).join('');
2288
+ if (!this.#clients.get(id)) {
2289
+ data.id = id;
2290
+ break;
2291
+ }
2292
+ }
2293
+ if (!data.id) {
2294
+ ws.send(encode({ type: 'id_taken' }), { binary: true });
2295
+ throw new Error('Failed to generate unique ID!');
2296
+ }
2297
+ }
2298
+
2299
+
2282
2300
  ws.clientId = data.id; // Adds the provided id to the ws object as clientId
2283
2301
  this.#clients.set(data.id, ws);
2284
2302
  const sessionToken = this.#generateSessionToken();
2285
2303
  this.#clientTokens.set(data.id, sessionToken); // Token is used when reconnecting to prevent impersonation attacks
2286
- ws.send(encode({ type: 'registered', sessionToken }), { binary: true });
2304
+ ws.send(encode({ type: 'registered', id: data.id, sessionToken }), { binary: true });
2287
2305
  this.#triggerEvent("clientRegistered", data.id, data.customData);
2288
2306
  break;
2289
2307
 
@@ -2432,7 +2450,7 @@ function requireServer () {
2432
2450
  break;
2433
2451
  }
2434
2452
  } catch (error) {
2435
- console.error('Error processing message:', error);
2453
+ console.error('Error in message handler:', error);
2436
2454
  }
2437
2455
  }
2438
2456
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "1.1.13",
3
+ "version": "1.2.0",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "module": "dist/playsocket-client.js",
6
6
  "exports": {