Browse Source

Add extra cases for catch-all '*' namespace

pull/1288/head
mooomooo 1 year ago
parent
commit
1c47c14fa2
  1. 63
      src/socketio/async_server.py
  2. 38
      src/socketio/server.py

63
src/socketio/async_server.py

@ -615,32 +615,55 @@ class AsyncServer(base_server.BaseServer):
await self.manager.trigger_callback(sid, id, data) await self.manager.trigger_callback(sid, id, data)
async def _trigger_event(self, event, namespace, *args): async def _trigger_event(self, event, namespace, *args):
"""Invoke an application event handler.""" """Invoke an application event handler.
Resolution priority:
- self.handlers[namespace][event]
- self.handlers[namespace]["*"]
- self.handlers["*"][event]
- self.handlers["*"]["*"]
- self.namespace_handlers[namespace]
- self.namespace_handlers["*"]
- self.not_handled
"""
handler = None
# first see if we have an explicit handler for the event # first see if we have an explicit handler for the event
if namespace in self.handlers: if namespace in self.handlers and \
handler = None event in self.handlers[namespace]:
if event in self.handlers[namespace]: handler = self.handlers[namespace][event]
handler = self.handlers[namespace][event] elif namespace in self.handlers and \
elif event not in self.reserved_events and \ event not in self.reserved_events and \
'*' in self.handlers[namespace]: '*' in self.handlers[namespace]:
handler = self.handlers[namespace]['*'] handler = self.handlers[namespace]['*']
args = (event, *args) args = (event, *args)
if handler: elif '*' in self.handlers and \
if asyncio.iscoroutinefunction(handler): event in self.handlers['*']:
try: handler = self.handlers['*'][event]
ret = await handler(*args) args = (namespace, *args)
except asyncio.CancelledError: # pragma: no cover elif '*' in self.handlers and \
ret = None event not in self.reserved_events and \
else: '*' in self.handlers['*']:
ret = handler(*args) handler = self.handlers['*']['*']
return ret args = (event, namespace, *args)
else:
return self.not_handled
# or else, forward the event to a namepsace handler if one exists # or else, forward the event to a namepsace handler if one exists
elif namespace in self.namespace_handlers: # pragma: no branch elif namespace in self.namespace_handlers: # pragma: no branch
return await self.namespace_handlers[namespace].trigger_event( return await self.namespace_handlers[namespace].trigger_event(
event, *args) event, *args)
elif '*' in self.namespace_handlers: # pragma: no branch
return await self.namespace_handlers['*'].trigger_event(
event, namespace, *args)
if handler:
if asyncio.iscoroutinefunction(handler):
try:
ret = await handler(*args)
except asyncio.CancelledError: # pragma: no cover
ret = None
else:
ret = handler(*args)
return ret
else:
return self.not_handled
async def _handle_eio_connect(self, eio_sid, environ): async def _handle_eio_connect(self, eio_sid, environ):
"""Handle the Engine.IO connection event.""" """Handle the Engine.IO connection event."""

38
src/socketio/server.py

@ -602,21 +602,41 @@ class Server(base_server.BaseServer):
self.manager.trigger_callback(sid, id, data) self.manager.trigger_callback(sid, id, data)
def _trigger_event(self, event, namespace, *args): def _trigger_event(self, event, namespace, *args):
"""Invoke an application event handler.""" """Invoke an application event handler.
Resolution priority:
- self.handlers[namespace][event]
- self.handlers[namespace]["*"]
- self.handlers["*"][event]
- self.handlers["*"]["*"]
- self.namespace_handlers[namespace]
- self.namespace_handlers["*"]
- self.not_handled
"""
# first see if we have an explicit handler for the event # first see if we have an explicit handler for the event
if namespace in self.handlers: if namespace in self.handlers and \
if event in self.handlers[namespace]: event in self.handlers[namespace]:
return self.handlers[namespace][event](*args) return self.handlers[namespace][event](*args)
elif event not in self.reserved_events and \ elif namespace in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers[namespace]: '*' in self.handlers[namespace]:
return self.handlers[namespace]['*'](event, *args) return self.handlers[namespace]['*'](event, *args)
else: elif '*' in self.handlers and \
return self.not_handled event in self.handlers['*']:
return self.handlers['*'][event](namespace, *args)
elif '*' in self.handlers and \
event not in self.reserved_events and \
'*' in self.handlers[namespace]:
return self.handlers['*']['*'](event, namespace, *args)
# or else, forward the event to a namespace handler if one exists # or else, forward the event to a namespace handler if one exists
elif namespace in self.namespace_handlers: # pragma: no branch elif namespace in self.namespace_handlers: # pragma: no branch
return self.namespace_handlers[namespace].trigger_event( return self.namespace_handlers[namespace].trigger_event(
event, *args) event, *args)
elif '*' in self.namespace_handlers: # pragma: no branch
return self.namespace_handlers['*'].trigger_event(
event, namespace, *args)
else:
return self.not_handled
def _handle_eio_connect(self, eio_sid, environ): def _handle_eio_connect(self, eio_sid, environ):
"""Handle the Engine.IO connection event.""" """Handle the Engine.IO connection event."""

Loading…
Cancel
Save