diff --git a/socketio/namespace.py b/socketio/namespace.py index fa10cf4..ab3a59b 100644 --- a/socketio/namespace.py +++ b/socketio/namespace.py @@ -55,7 +55,11 @@ class Namespace(object): setattr(self, func_name, get_wrapped_method(func_name)) def get_event_handler(self, event_name): - """Returns the event handler for requested event or ``None``.""" + """Returns the event handler for given ``event`` in this namespace or + ``None``, if none exists. + + :param event: The event name the handler is required for. + """ for attr_name in dir(self): attr = getattr(self, attr_name) if hasattr(attr, '_event_name'): diff --git a/socketio/server.py b/socketio/server.py index bce6aa8..976dada 100644 --- a/socketio/server.py +++ b/socketio/server.py @@ -171,6 +171,21 @@ class Server(object): self.handlers[name] = namespace return namespace + def get_event_handler(self, event, namespace): + """Returns the event handler for given ``event`` and ``namespace`` or + ``None``, if none exists. + + :param event: The event name the handler is required for. + :param namespace: The Socket.IO namespace for the event. + """ + handler = None + ns = self.handlers.get(namespace) + if isinstance(ns, sio_namespace.Namespace): + handler = ns.get_event_handler(event) + elif isinstance(ns, dict): + handler = ns.get(event) + return handler + def emit(self, event, data=None, room=None, skip_sid=None, namespace=None, callback=None): """Emit a custom event to one or more connected clients. @@ -445,12 +460,7 @@ class Server(object): def _trigger_event(self, event, namespace, *args): """Invoke an application event handler.""" - handler = None - ns = self.handlers.get(namespace) - if isinstance(ns, sio_namespace.Namespace): - handler = ns.get_event_handler(event) - elif isinstance(ns, dict): - handler = ns.get(event) + handler = self.get_event_handler(event, namespace) if handler is not None: return handler(*args)