Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

192 linhas
7.7KB

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