playsocketjs 2.6.3 → 2.6.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.
@@ -2257,6 +2257,9 @@ class PlaySocketServer {
2257
2257
  const formerRoomId = this.#clientRooms.get(data.id);
2258
2258
  const formerRoom = this.#rooms[formerRoomId];
2259
2259
  if (formerRoom) {
2260
+ // If the room has no host (they were the only one and disconnected), restore them as host
2261
+ if (formerRoom.host === null) formerRoom.host = data.id;
2262
+
2260
2263
  if (this.#debug) console.log(`State sent for reconnection for room ${formerRoomId}:`, formerRoom.crdtManager.getState);
2261
2264
  roomData = {
2262
2265
  state: formerRoom.crdtManager.getState,
@@ -2339,6 +2342,17 @@ class PlaySocketServer {
2339
2342
  version: this.#roomVersions.get(roomId)
2340
2343
  }), { binary: true });
2341
2344
 
2345
+ // If the room has no host (previous host disconnected and was the only participant), make joiner the host
2346
+ // Since the room had to be empty, only the joiner needs to be notified
2347
+ const becameHost = room.host === null;
2348
+ if (becameHost) {
2349
+ room.host = ws.clientId;
2350
+ ws.send(encode({
2351
+ type: 'host_migrated',
2352
+ newHost: room.host,
2353
+ }), { binary: true });
2354
+ }
2355
+
2342
2356
  // Notify existing participants
2343
2357
  room.participants.forEach(p => {
2344
2358
  if (p === ws.clientId) return;
@@ -2427,18 +2441,21 @@ class PlaySocketServer {
2427
2441
  */
2428
2442
  #changeHostIfDisconnected(roomId, clientId) {
2429
2443
  const room = this.#rooms[roomId];
2430
- if (room && room.host === clientId && room.participants.length > 1) {
2444
+ if (room && room.host === clientId) {
2431
2445
  const participantsWithoutClient = room.participants.filter((e) => e !== clientId);
2432
- room.host = participantsWithoutClient[0]; // Set new host
2433
2446
 
2434
- // Inform all participants about the new host
2435
- participantsWithoutClient.forEach(p => {
2436
- const client = this.#clients.get(p);
2437
- if (client) client.send(encode({
2438
- type: 'host_migrated',
2439
- newHost: room.host,
2440
- }), { binary: true });
2441
- });
2447
+ if (participantsWithoutClient.length >= 1) {
2448
+ room.host = participantsWithoutClient[0]; // Set new host
2449
+
2450
+ // Inform all participants about the new host
2451
+ participantsWithoutClient.forEach(p => {
2452
+ const client = this.#clients.get(p);
2453
+ if (client) client.send(encode({
2454
+ type: 'host_migrated',
2455
+ newHost: room.host,
2456
+ }), { binary: true });
2457
+ });
2458
+ } else room.host = null; // The next person to join will become the host
2442
2459
  }
2443
2460
  }
2444
2461
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "2.6.3",
3
+ "version": "2.6.4",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "type": "module",
6
6
  "module": "dist/playsocket-client.js",