playsocketjs 5.3.0 → 5.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "5.3.0",
3
+ "version": "5.3.1",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "type": "module",
6
6
  "module": "./src/client/client.js",
@@ -213,9 +213,11 @@ export default class PlaySocketServer {
213
213
  }
214
214
 
215
215
  case "reconnect": {
216
- // If user is pending disconnect, respond (otherwise it's too late)
217
- const pd = this.#pendingDisconnects.get(data.id);
218
- if (pd && data.sessionToken) {
216
+ // If user is pending disconnect or still on a dropped connection, respond (otherwise it's too late)
217
+ const pendingDisconnect = this.#pendingDisconnects.get(data.id);
218
+ const staleSocket = this.#clients.get(data.id); // Client requests reconnect even though the server thinks they are still connected
219
+
220
+ if ((pendingDisconnect || staleSocket) && data.sessionToken) {
219
221
  if (data.sessionToken !== this.#clientTokens.get(data.id)) {
220
222
  ws.send(encode({
221
223
  type: "reconnection_failed",
@@ -223,9 +225,15 @@ export default class PlaySocketServer {
223
225
  }), { binary: true });
224
226
  return;
225
227
  }
226
- clearTimeout(pd.timeout);
228
+ clearTimeout(pendingDisconnect?.timeout);
227
229
  this.#pendingDisconnects.delete(data.id);
228
230
 
231
+ // Only terminate if it's a different connection sending the reconnect
232
+ if (staleSocket && staleSocket !== ws) {
233
+ staleSocket.clientId = null; // So the close skips disconnection (handleDisconnection exits if the client ID is falsy)
234
+ staleSocket.terminate();
235
+ }
236
+
229
237
  // Re-assign old client ID to the new ws connection
230
238
  ws.clientId = data.id;
231
239
  this.#clients.set(data.id, ws);
@@ -416,9 +424,7 @@ export default class PlaySocketServer {
416
424
  * @returns {string} Token
417
425
  */
418
426
  #generateSessionToken() {
419
- let token = "";
420
- for (let i = 0; i < 16; i++) token += Math.floor(Math.random() * 16).toString(16);
421
- return token;
427
+ return crypto.randomUUID();
422
428
  }
423
429
 
424
430
  /**
@@ -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 = "5.3.0";
12
+ export const VERSION = "5.3.1";
13
13
 
14
14
  /**
15
15
  * Logging prefixes
@@ -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 = "5.3.0";
11
+ export declare const VERSION = "5.3.1";
12
12
  /**
13
13
  * Logging prefixes
14
14
  */