Browse Source

Unified get_event_handler(event_name) so that it can be overloaded on both

server and namespace level.
pull/43/merge^2
Robert Schindler 9 years ago
parent
commit
5f51c7ffa8
  1. 6
      socketio/namespace.py
  2. 22
      socketio/server.py

6
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'):

22
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)

Loading…
Cancel
Save