export class WsRooms {
    rooms = new Map();
    clientRooms = new Map();
    subscribe(ws, room) {
        if (!this.rooms.has(room)) {
            this.rooms.set(room, new Set());
        }
        this.rooms.get(room).add(ws);
        if (!this.clientRooms.has(ws)) {
            this.clientRooms.set(ws, new Set());
        }
        this.clientRooms.get(ws).add(room);
    }
    unsubscribe(ws, room) {
        this.rooms.get(room)?.delete(ws);
        this.clientRooms.get(ws)?.delete(room);
    }
    removeClient(ws) {
        const rooms = this.clientRooms.get(ws);
        if (rooms) {
            for (const room of rooms) {
                this.rooms.get(room)?.delete(ws);
            }
        }
        this.clientRooms.delete(ws);
    }
    broadcast(room, data) {
        const clients = this.rooms.get(room);
        if (!clients)
            return;
        for (const ws of clients) {
            if (ws.readyState === ws.OPEN) {
                ws.send(data);
            }
        }
    }
    broadcastAll(data) {
        const sent = new Set();
        for (const [, clients] of this.rooms) {
            for (const ws of clients) {
                if (!sent.has(ws) && ws.readyState === ws.OPEN) {
                    ws.send(data);
                    sent.add(ws);
                }
            }
        }
    }
    getSubscribers(room) {
        return this.rooms.get(room) || new Set();
    }
}
//# sourceMappingURL=ws-rooms.js.map