Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

59 строки
2.0KB

  1. import asyncio
  2. from .base_manager import BaseManager
  3. class AsyncManager(BaseManager):
  4. """Manage a client list for an asyncio server."""
  5. async def emit(self, event, data, namespace, room=None, skip_sid=None,
  6. callback=None, **kwargs):
  7. """Emit a message to a single client, a room, or all the clients
  8. connected to the namespace.
  9. Note: this method is a coroutine.
  10. """
  11. if namespace not in self.rooms or room not in self.rooms[namespace]:
  12. return
  13. tasks = []
  14. if not isinstance(skip_sid, list):
  15. skip_sid = [skip_sid]
  16. for sid in self.get_participants(namespace, room):
  17. if sid not in skip_sid:
  18. if callback is not None:
  19. id = self._generate_ack_id(sid, namespace, callback)
  20. else:
  21. id = None
  22. tasks.append(self.server._emit_internal(sid, event, data,
  23. namespace, id))
  24. if tasks == []: # pragma: no cover
  25. return
  26. await asyncio.wait(tasks)
  27. async def close_room(self, room, namespace):
  28. """Remove all participants from a room.
  29. Note: this method is a coroutine.
  30. """
  31. return super().close_room(room, namespace)
  32. async def trigger_callback(self, sid, namespace, id, data):
  33. """Invoke an application callback.
  34. Note: this method is a coroutine.
  35. """
  36. callback = None
  37. try:
  38. callback = self.callbacks[sid][namespace][id]
  39. except KeyError:
  40. # if we get an unknown callback we just ignore it
  41. self._get_logger().warning('Unknown callback received, ignoring.')
  42. else:
  43. del self.callbacks[sid][namespace][id]
  44. if callback is not None:
  45. ret = callback(*data)
  46. if asyncio.iscoroutine(ret):
  47. try:
  48. await ret
  49. except asyncio.CancelledError: # pragma: no cover
  50. pass