playsocketjs 1.0.2 → 1.0.3

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
@@ -134,7 +134,6 @@ const server = new PlaySocketServer();
134
134
 
135
135
  // Gracefully disconnect all clients and close the server (optional)
136
136
  function shutdown() {
137
- console.log('Shutting down gracefully...');
138
137
  server.stop();
139
138
  process.exit(0);
140
139
  }
@@ -169,7 +168,6 @@ httpServer.listen(port, () => {
169
168
 
170
169
  // Gracefully disconnect all clients and close the server (optional)
171
170
  function shutdown() {
172
- console.log('Shutting down gracefully...');
173
171
  server.stop();
174
172
  process.exit(0);
175
173
  }
@@ -4,7 +4,7 @@
4
4
 
5
5
  const ERROR_PREFIX = "PlaySocket error: ";
6
6
  const WARNING_PREFIX = "PlaySocket warning: ";
7
- const TIMEOUT_MS = 3000; // 3 second timeout for operations
7
+ const TIMEOUT_MS = 5000; // 5 second timeout for operations
8
8
 
9
9
  class PlaySocket {
10
10
  // Core properties
@@ -29,6 +29,9 @@ class PlaySocket {
29
29
  #pendingHost;
30
30
  #pendingInit;
31
31
 
32
+ // Timeouts or intervals
33
+ #reconnectTimeout;
34
+
32
35
  /**
33
36
  * Create a new PlaySocket instance
34
37
  * @param {string} id - Unique identifier for this client
@@ -227,10 +230,10 @@ class PlaySocket {
227
230
 
228
231
  // Handle socket close & attempt reconnect
229
232
  this.#socket.onclose = () => {
233
+ if (!this.#initialized) return;
230
234
  this.#triggerEvent("status", "Disconnected from server.");
231
235
 
232
- setTimeout(async () => {
233
- if (!this.#initialized || !this.#socket) return;
236
+ this.#reconnectTimeout = setTimeout(async () => {
234
237
  try {
235
238
  await this.#connect(); // Will set status to connected if successful
236
239
 
@@ -241,7 +244,6 @@ class PlaySocket {
241
244
  }
242
245
  } catch (error) {
243
246
  this.#triggerEvent("error", "WebSocket connection permanently closed: " + error);
244
- this.#socket = null;
245
247
  this.destroy();
246
248
  }
247
249
  }, 1000);
@@ -400,6 +402,8 @@ class PlaySocket {
400
402
  * Destroy the PlaySocket instance and disconnect
401
403
  */
402
404
  destroy() {
405
+ this.#initialized = false; // Set this immediately to prevent automatic reconnection
406
+
403
407
  if (this.#socket) {
404
408
  this.#socket.close();
405
409
  this.#socket = null;
@@ -410,12 +414,12 @@ class PlaySocket {
410
414
  }
411
415
 
412
416
  // Reset state
413
- this.#initialized = false;
414
417
  this.#isHost = false;
415
418
  this.#roomHost = null;
416
419
  this.#storage = {};
417
420
  this.#roomId = null;
418
421
  this.#connectionCount = 0;
422
+ clearTimeout(this.#reconnectTimeout);
419
423
  }
420
424
 
421
425
  // Public getters
@@ -335,7 +335,7 @@ function requireServer () {
335
335
  * Close all client connections, then close the websocket and http server
336
336
  */
337
337
  stop() {
338
- if (this.#heartbeatInterval) clearInterval(this.#heartbeatInterval);
338
+ clearInterval(this.#heartbeatInterval);
339
339
  if (this.#wss) {
340
340
  this.#wss.clients.forEach(client => client.close());
341
341
  this.#wss.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "module": "dist/playsocket-client.js",
6
6
  "exports": {