File size: 791 Bytes
c35855b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # In-memory room management
# rooms = { "room_id": [socket_id1, socket_id2, ...] }
rooms = {}
def add_user_to_room(room_id: str, socket_id: str):
"""Add a socket_id to the given room."""
if room_id not in rooms:
rooms[room_id] = []
if socket_id not in rooms[room_id]:
rooms[room_id].append(socket_id)
def remove_user_from_room(socket_id: str):
"""Remove a socket_id from ALL rooms it belongs to."""
for room_id in list(rooms.keys()):
if socket_id in rooms[room_id]:
rooms[room_id].remove(socket_id)
# Clean up empty rooms
if not rooms[room_id]:
del rooms[room_id]
def get_room_members(room_id: str) -> list:
"""Return list of socket IDs in a room."""
return rooms.get(room_id, [])
|