playsocketjs 3.1.0 → 3.1.2
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/dist/playsocket-client.js +10 -10
- package/dist/playsocket-server.js +29 -20
- package/package.json +1 -1
|
@@ -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
|
|
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 + "
|
|
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", "
|
|
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", "
|
|
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", "
|
|
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);
|
|
@@ -2298,7 +2298,6 @@ class PlaySocketServer {
|
|
|
2298
2298
|
if (formerRoom) {
|
|
2299
2299
|
// If the room has no host (they were the only one and disconnected), restore them as host
|
|
2300
2300
|
if (formerRoom.host === null) formerRoom.host = data.id;
|
|
2301
|
-
|
|
2302
2301
|
if (this.#debug) console.log(`State sent for reconnection for room ${formerRoomId}:`, formerRoom.crdtManager.state);
|
|
2303
2302
|
roomData = {
|
|
2304
2303
|
state: formerRoom.crdtManager.state,
|
|
@@ -2334,7 +2333,7 @@ class PlaySocketServer {
|
|
|
2334
2333
|
|
|
2335
2334
|
const newRoom = this.createRoom(data.initialStorage, data.size, ws.clientId); // Create room
|
|
2336
2335
|
|
|
2337
|
-
//
|
|
2336
|
+
// Verify client is still connected and abort if not
|
|
2338
2337
|
if (!this.#clients.has(ws.clientId)) {
|
|
2339
2338
|
this.destroyRoom(newRoom.id);
|
|
2340
2339
|
if (this.#debug) console.log(`Room creation cancelled - client ${ws.clientId} disconnected during event callback.`);
|
|
@@ -2363,6 +2362,12 @@ class PlaySocketServer {
|
|
|
2363
2362
|
const joinAllowed = await this.#triggerEvent("clientJoinRequested", ws.clientId, roomId);
|
|
2364
2363
|
if (joinAllowed === false || typeof joinAllowed === 'string') return rejectJoin(typeof joinAllowed === 'string' ? joinAllowed : 'Denied.');
|
|
2365
2364
|
|
|
2365
|
+
// Verify client is still connected and abort if not
|
|
2366
|
+
if (!this.#clients.has(ws.clientId)) {
|
|
2367
|
+
if (this.#debug) console.log(`Room join cancelled - client ${ws.clientId} disconnected during event callback.`);
|
|
2368
|
+
return;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2366
2371
|
try {
|
|
2367
2372
|
this.#joinRoom(ws.clientId, roomId);
|
|
2368
2373
|
} catch (error) {
|
|
@@ -2578,7 +2583,7 @@ class PlaySocketServer {
|
|
|
2578
2583
|
const oldRoomId = this.#clientRooms.get(clientId);
|
|
2579
2584
|
const oldRoom = this.#rooms[oldRoomId];
|
|
2580
2585
|
const targetRoom = this.#rooms[roomId];
|
|
2581
|
-
if (!client) throw new Error("Client not found.");
|
|
2586
|
+
if (!client && !this.#pendingDisconnects.has(clientId)) throw new Error("Client not found.");
|
|
2582
2587
|
if (!oldRoomId) throw new Error("Client is not in a room.");
|
|
2583
2588
|
if (!oldRoom) throw new Error("Client room not found.");
|
|
2584
2589
|
if (oldRoomId === roomId) throw new Error("Client is already in target room.");
|
|
@@ -2698,33 +2703,37 @@ class PlaySocketServer {
|
|
|
2698
2703
|
const room = this.#rooms[roomId];
|
|
2699
2704
|
const client = this.#clients.get(clientId);
|
|
2700
2705
|
if (!room) throw new Error("Room not found.");
|
|
2701
|
-
if (!client) throw new Error("Client not found.");
|
|
2706
|
+
if (!client && !this.#pendingDisconnects.has(clientId)) throw new Error("Client not found.");
|
|
2702
2707
|
if (this.#clientRooms.get(clientId)) throw new Error("Already in a room.");
|
|
2703
2708
|
if (room.participants.length >= room.size) throw new Error("Room full.");
|
|
2704
2709
|
|
|
2705
2710
|
room.participants.push(clientId);
|
|
2706
2711
|
this.#clientRooms.set(clientId, roomId);
|
|
2707
2712
|
|
|
2708
|
-
// Notify joiner with initial storage
|
|
2709
|
-
if (
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2713
|
+
// Notify joiner with initial storage (only if connected - pending disconnect clients get state on reconnect)
|
|
2714
|
+
if (client) {
|
|
2715
|
+
if (this.#debug) console.log("Room state sent for join:", room.crdtManager.state);
|
|
2716
|
+
client.send(encode({
|
|
2717
|
+
type: 'join_accepted',
|
|
2718
|
+
roomId,
|
|
2719
|
+
state: room.crdtManager.state,
|
|
2720
|
+
participantCount: room.participants.length,
|
|
2721
|
+
host: room.host,
|
|
2722
|
+
version: this.#roomVersions.get(roomId)
|
|
2723
|
+
}), { binary: true });
|
|
2724
|
+
}
|
|
2718
2725
|
|
|
2719
2726
|
// If the room has no host (previous host disconnected and was the only participant), make joiner the host
|
|
2720
|
-
//
|
|
2727
|
+
// Set new host after join_accepted so hostMigrated event fires correctly on client
|
|
2721
2728
|
const becameHost = room.host === null;
|
|
2722
2729
|
if (becameHost) {
|
|
2723
|
-
room.host = clientId;
|
|
2724
|
-
client
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2730
|
+
room.host = clientId; // Change host
|
|
2731
|
+
if (client) {
|
|
2732
|
+
client.send(encode({
|
|
2733
|
+
type: 'host_migrated',
|
|
2734
|
+
newHost: room.host,
|
|
2735
|
+
}), { binary: true });
|
|
2736
|
+
}
|
|
2728
2737
|
}
|
|
2729
2738
|
|
|
2730
2739
|
// Notify existing participants
|