playsocketjs 1.0.2 → 1.0.4
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 +0 -2
- package/dist/playsocket-client.js +14 -8
- package/dist/playsocket-server.js +1 -1
- package/package.json +1 -1
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 =
|
|
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);
|
|
@@ -331,13 +333,14 @@ class PlaySocket {
|
|
|
331
333
|
*/
|
|
332
334
|
updateStorage(key, value) {
|
|
333
335
|
if (JSON.stringify(this.#storage[key]) === JSON.stringify(value)) return;
|
|
334
|
-
this.#storage[key] = value;
|
|
335
|
-
this.#triggerEvent("storageUpdated", { ...this.#storage });
|
|
336
336
|
this.#sendToServer({
|
|
337
337
|
type: 'room_storage_update',
|
|
338
338
|
key,
|
|
339
339
|
value
|
|
340
340
|
});
|
|
341
|
+
// Optimistic update AFTER sendToServer to preserve order if messages are being sent in the event callback
|
|
342
|
+
this.#storage[key] = value;
|
|
343
|
+
this.#triggerEvent("storageUpdated", { ...this.#storage });
|
|
341
344
|
}
|
|
342
345
|
|
|
343
346
|
/**
|
|
@@ -348,7 +351,6 @@ class PlaySocket {
|
|
|
348
351
|
* @param {*} updateValue - New value for update-matching
|
|
349
352
|
*/
|
|
350
353
|
updateStorageArray(key, operation, value, updateValue) {
|
|
351
|
-
this.#handleArrayUpdate(key, operation, value, updateValue);
|
|
352
354
|
this.#sendToServer({
|
|
353
355
|
type: 'room_storage_array_update',
|
|
354
356
|
key,
|
|
@@ -356,6 +358,8 @@ class PlaySocket {
|
|
|
356
358
|
value,
|
|
357
359
|
updateValue
|
|
358
360
|
});
|
|
361
|
+
// Optimistic update AFTER sendToServer to preserve order if messages are being sent in the event callback
|
|
362
|
+
this.#handleArrayUpdate(key, operation, value, updateValue);
|
|
359
363
|
}
|
|
360
364
|
|
|
361
365
|
/**
|
|
@@ -400,6 +404,8 @@ class PlaySocket {
|
|
|
400
404
|
* Destroy the PlaySocket instance and disconnect
|
|
401
405
|
*/
|
|
402
406
|
destroy() {
|
|
407
|
+
this.#initialized = false; // Set this immediately to prevent automatic reconnection
|
|
408
|
+
|
|
403
409
|
if (this.#socket) {
|
|
404
410
|
this.#socket.close();
|
|
405
411
|
this.#socket = null;
|
|
@@ -410,12 +416,12 @@ class PlaySocket {
|
|
|
410
416
|
}
|
|
411
417
|
|
|
412
418
|
// Reset state
|
|
413
|
-
this.#initialized = false;
|
|
414
419
|
this.#isHost = false;
|
|
415
420
|
this.#roomHost = null;
|
|
416
421
|
this.#storage = {};
|
|
417
422
|
this.#roomId = null;
|
|
418
423
|
this.#connectionCount = 0;
|
|
424
|
+
clearTimeout(this.#reconnectTimeout);
|
|
419
425
|
}
|
|
420
426
|
|
|
421
427
|
// 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
|
-
|
|
338
|
+
clearInterval(this.#heartbeatInterval);
|
|
339
339
|
if (this.#wss) {
|
|
340
340
|
this.#wss.clients.forEach(client => client.close());
|
|
341
341
|
this.#wss.close();
|