No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

205 líneas
8.0KB

  1. import asyncio
  2. from socketio import namespace
  3. class AsyncNamespace(namespace.Namespace):
  4. """Base class for asyncio server-side class-based namespaces.
  5. A class-based namespace is a class that contains all the event handlers
  6. for a Socket.IO namespace. The event handlers are methods of the class
  7. with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``,
  8. ``on_message``, ``on_json``, and so on. These can be regular functions or
  9. coroutines.
  10. :param namespace: The Socket.IO namespace to be used with all the event
  11. handlers defined in this class. If this argument is
  12. omitted, the default namespace is used.
  13. """
  14. def is_asyncio_based(self):
  15. return True
  16. async def trigger_event(self, event, *args):
  17. """Dispatch an event to the proper handler method.
  18. In the most common usage, this method is not overloaded by subclasses,
  19. as it performs the routing of events to methods. However, this
  20. method can be overriden if special dispatching rules are needed, or if
  21. having a single method that catches all events is desired.
  22. Note: this method is a coroutine.
  23. """
  24. handler_name = 'on_' + event
  25. if hasattr(self, handler_name):
  26. handler = getattr(self, handler_name)
  27. if asyncio.iscoroutinefunction(handler) is True:
  28. try:
  29. ret = await handler(*args)
  30. except asyncio.CancelledError: # pragma: no cover
  31. ret = None
  32. else:
  33. ret = handler(*args)
  34. return ret
  35. async def emit(self, event, data=None, room=None, skip_sid=None,
  36. namespace=None, callback=None):
  37. """Emit a custom event to one or more connected clients.
  38. The only difference with the :func:`socketio.Server.emit` method is
  39. that when the ``namespace`` argument is not given the namespace
  40. associated with the class is used.
  41. Note: this method is a coroutine.
  42. """
  43. return await self.server.emit(event, data=data, room=room,
  44. skip_sid=skip_sid,
  45. namespace=namespace or self.namespace,
  46. callback=callback)
  47. async def send(self, data, room=None, skip_sid=None, namespace=None,
  48. callback=None):
  49. """Send a message to one or more connected clients.
  50. The only difference with the :func:`socketio.Server.send` method is
  51. that when the ``namespace`` argument is not given the namespace
  52. associated with the class is used.
  53. Note: this method is a coroutine.
  54. """
  55. return await self.server.send(data, room=room, skip_sid=skip_sid,
  56. namespace=namespace or self.namespace,
  57. callback=callback)
  58. async def close_room(self, room, namespace=None):
  59. """Close a room.
  60. The only difference with the :func:`socketio.Server.close_room` method
  61. is that when the ``namespace`` argument is not given the namespace
  62. associated with the class is used.
  63. Note: this method is a coroutine.
  64. """
  65. return await self.server.close_room(
  66. room, namespace=namespace or self.namespace)
  67. async def get_session(self, sid, namespace=None):
  68. """Return the user session for a client.
  69. The only difference with the :func:`socketio.Server.get_session`
  70. method is that when the ``namespace`` argument is not given the
  71. namespace associated with the class is used.
  72. Note: this method is a coroutine.
  73. """
  74. return await self.server.get_session(
  75. sid, namespace=namespace or self.namespace)
  76. async def save_session(self, sid, session, namespace=None):
  77. """Store the user session for a client.
  78. The only difference with the :func:`socketio.Server.save_session`
  79. method is that when the ``namespace`` argument is not given the
  80. namespace associated with the class is used.
  81. Note: this method is a coroutine.
  82. """
  83. return await self.server.save_session(
  84. sid, session, namespace=namespace or self.namespace)
  85. def session(self, sid, namespace=None):
  86. """Return the user session for a client with context manager syntax.
  87. The only difference with the :func:`socketio.Server.session` method is
  88. that when the ``namespace`` argument is not given the namespace
  89. associated with the class is used.
  90. """
  91. return self.server.session(sid, namespace=namespace or self.namespace)
  92. async def disconnect(self, sid, namespace=None):
  93. """Disconnect a client.
  94. The only difference with the :func:`socketio.Server.disconnect` method
  95. is that when the ``namespace`` argument is not given the namespace
  96. associated with the class is used.
  97. Note: this method is a coroutine.
  98. """
  99. return await self.server.disconnect(
  100. sid, namespace=namespace or self.namespace)
  101. class AsyncClientNamespace(namespace.ClientNamespace):
  102. """Base class for asyncio client-side class-based namespaces.
  103. A class-based namespace is a class that contains all the event handlers
  104. for a Socket.IO namespace. The event handlers are methods of the class
  105. with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``,
  106. ``on_message``, ``on_json``, and so on. These can be regular functions or
  107. coroutines.
  108. :param namespace: The Socket.IO namespace to be used with all the event
  109. handlers defined in this class. If this argument is
  110. omitted, the default namespace is used.
  111. """
  112. def is_asyncio_based(self):
  113. return True
  114. async def trigger_event(self, event, *args):
  115. """Dispatch an event to the proper handler method.
  116. In the most common usage, this method is not overloaded by subclasses,
  117. as it performs the routing of events to methods. However, this
  118. method can be overriden if special dispatching rules are needed, or if
  119. having a single method that catches all events is desired.
  120. Note: this method is a coroutine.
  121. """
  122. handler_name = 'on_' + event
  123. if hasattr(self, handler_name):
  124. handler = getattr(self, handler_name)
  125. if asyncio.iscoroutinefunction(handler) is True:
  126. try:
  127. ret = await handler(*args)
  128. except asyncio.CancelledError: # pragma: no cover
  129. ret = None
  130. else:
  131. ret = handler(*args)
  132. return ret
  133. async def emit(self, event, data=None, namespace=None, callback=None):
  134. """Emit a custom event to the server.
  135. The only difference with the :func:`socketio.Client.emit` method is
  136. that when the ``namespace`` argument is not given the namespace
  137. associated with the class is used.
  138. Note: this method is a coroutine.
  139. """
  140. return await self.client.emit(event, data=data,
  141. namespace=namespace or self.namespace,
  142. callback=callback)
  143. async def send(self, data, namespace=None, callback=None):
  144. """Send a message to the server.
  145. The only difference with the :func:`socketio.Client.send` method is
  146. that when the ``namespace`` argument is not given the namespace
  147. associated with the class is used.
  148. Note: this method is a coroutine.
  149. """
  150. return await self.client.send(data,
  151. namespace=namespace or self.namespace,
  152. callback=callback)
  153. async def disconnect(self):
  154. """Disconnect a client.
  155. The only difference with the :func:`socketio.Client.disconnect` method
  156. is that when the ``namespace`` argument is not given the namespace
  157. associated with the class is used.
  158. Note: this method is a coroutine.
  159. """
  160. return await self.client.disconnect()