From 3ddadcf846a4003e17dfd5f85dd30231e097aeff Mon Sep 17 00:00:00 2001 From: elia Bottos Date: Sun, 24 Jul 2022 01:12:24 +0200 Subject: [PATCH] add clients_room in server.py and client_in_room in base_manager.py to get the number of clients in a room --- src/socketio/base_manager.py | 7 +++++++ src/socketio/server.py | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/socketio/base_manager.py b/src/socketio/base_manager.py index 0d6e1a9..c90d72e 100644 --- a/src/socketio/base_manager.py +++ b/src/socketio/base_manager.py @@ -154,6 +154,13 @@ class BaseManager(object): pass return r + def client_in_room(self, room_in, namespace): + """Return the number of client in room""" + try: + return len(self.rooms[namespace][room_in]) + except KeyError: + pass + def emit(self, event, data, namespace, room=None, skip_sid=None, callback=None, **kwargs): """Emit a message to a single client, a room, or all the clients diff --git a/src/socketio/server.py b/src/socketio/server.py index 0456ed6..6a7c358 100644 --- a/src/socketio/server.py +++ b/src/socketio/server.py @@ -472,6 +472,16 @@ class Server(object): namespace = namespace or '/' return self.manager.get_rooms(sid, namespace) + def clients_room(self, room, namespace=None): + """Return the number of clients in a room. + + :param room: room selected. + :param namespace: The Socket.IO namespace for the event. If this + argument is omitted the default namespace is used. + """ + namespace = namespace or '/' + return self.manager.client_in_room(room, namespace) + def get_session(self, sid, namespace=None): """Return the user session for a client.