class ConnectionManager { constructor() { this.users = new Map(); } /** * Adds a new user to the active connections. * Returns false if the user already exists. */ addUser(username, ws) { if (this.users.has(username)) { return false; } this.users.set(username, ws); return true; } /** * Remove a user by username. */ removeUser(username) { this.users.delete(username); } /** * Get the socket connection for a given username. */ getUserSocket(username) { return this.users.get(username); } /** * Broadcasts a message to all connected users except the supplied username. */ broadcast(message, excludeUser = null) { for (const [username, ws] of this.users.entries()) { if (username !== excludeUser && ws.readyState === ws.OPEN) { ws.send(JSON.stringify(message)); } } } } module.exports = ConnectionManager;