diff --git a/src/network/broadcastManager.js b/src/network/broadcastManager.js index 7be5e43..93f8386 100644 --- a/src/network/broadcastManager.js +++ b/src/network/broadcastManager.js @@ -17,7 +17,14 @@ class BroadcastManager { * Broadcast connected user count to all clients */ broadcastUserCount() { - const count = this.io.engine ? this.io.engine.clientsCount : 0; + // Try multiple ways to get the count + let count = 0; + if (this.io.sockets && this.io.sockets.sockets) { + count = this.io.sockets.sockets.size || 0; + } else if (this.io.engine) { + count = this.io.engine.clientsCount || 0; + } + // Only broadcast if count changed to avoid spam if (count !== this._lastUserCount) { this._lastUserCount = count; diff --git a/src/network/connectionHandler.js b/src/network/connectionHandler.js index 1aa8c18..00ed1be 100644 --- a/src/network/connectionHandler.js +++ b/src/network/connectionHandler.js @@ -64,8 +64,8 @@ class ConnectionHandler { // Send current state this._sendConnectionStatus(socket); - // Broadcast updated user count to all clients - this.broadcastManager.broadcastUserCount(); + // Broadcast updated user count to all clients (small delay to ensure socket is registered) + setTimeout(() => this.broadcastManager.broadcastUserCount(), 100); return { sessionInfo, memUser };