codekingpro/portable-devtools
114k
1from . import base_namespace2 3 4class Namespace(base_namespace.BaseServerNamespace):5 """Base class for server-side class-based namespaces.6 7 A class-based namespace is a class that contains all the event handlers8 for a Socket.IO namespace. The event handlers are methods of the class9 with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``,10 ``on_message``, ``on_json``, and so on.11 12 :param namespace: The Socket.IO namespace to be used with all the event13 handlers defined in this class. If this argument is14 omitted, the default namespace is used.15 """16 def trigger_event(self, event, *args):17 """Dispatch an event to the proper handler method.18 19 In the most common usage, this method is not overloaded by subclasses,20 as it performs the routing of events to methods. However, this21 method can be overridden if special dispatching rules are needed, or if22 having a single method that catches all events is desired.23 """24 handler_name = 'on_' + event25 if hasattr(self, handler_name):26 return getattr(self, handler_name)(*args)27 28 def emit(self, event, data=None, to=None, room=None, skip_sid=None,29 namespace=None, callback=None, ignore_queue=False):30 """Emit a custom event to one or more connected clients.31 32 The only difference with the :func:`socketio.Server.emit` method is33 that when the ``namespace`` argument is not given the namespace34 associated with the class is used.35 """36 return self.server.emit(event, data=data, to=to, room=room,37 skip_sid=skip_sid,38 namespace=namespace or self.namespace,39 callback=callback, ignore_queue=ignore_queue)40 41 def send(self, data, to=None, room=None, skip_sid=None, namespace=None,42 callback=None, ignore_queue=False):43 """Send a message to one or more connected clients.44 45 The only difference with the :func:`socketio.Server.send` method is46 that when the ``namespace`` argument is not given the namespace47 associated with the class is used.48 """49 return self.server.send(data, to=to, room=room, skip_sid=skip_sid,50 namespace=namespace or self.namespace,51 callback=callback, ignore_queue=ignore_queue)52 53 def call(self, event, data=None, to=None, sid=None, namespace=None,54 timeout=None, ignore_queue=False):55 """Emit a custom event to a client and wait for the response.56 57 The only difference with the :func:`socketio.Server.call` method is58 that when the ``namespace`` argument is not given the namespace59 associated with the class is used.60 """61 return self.server.call(event, data=data, to=to, sid=sid,62 namespace=namespace or self.namespace,63 timeout=timeout, ignore_queue=ignore_queue)64 65 def enter_room(self, sid, room, namespace=None):66 """Enter a room.67 68 The only difference with the :func:`socketio.Server.enter_room` method69 is that when the ``namespace`` argument is not given the namespace70 associated with the class is used.71 """72 return self.server.enter_room(sid, room,73 namespace=namespace or self.namespace)74 75 def leave_room(self, sid, room, namespace=None):76 """Leave a room.77 78 The only difference with the :func:`socketio.Server.leave_room` method79 is that when the ``namespace`` argument is not given the namespace80 associated with the class is used.81 """82 return self.server.leave_room(sid, room,83 namespace=namespace or self.namespace)84 85 def close_room(self, room, namespace=None):86 """Close a room.87 88 The only difference with the :func:`socketio.Server.close_room` method89 is that when the ``namespace`` argument is not given the namespace90 associated with the class is used.91 """92 return self.server.close_room(room,93 namespace=namespace or self.namespace)94 95 def get_session(self, sid, namespace=None):96 """Return the user session for a client.97 98 The only difference with the :func:`socketio.Server.get_session`99 method is that when the ``namespace`` argument is not given the100 namespace associated with the class is used.101 """102 return self.server.get_session(103 sid, namespace=namespace or self.namespace)104 105 def save_session(self, sid, session, namespace=None):106 """Store the user session for a client.107 108 The only difference with the :func:`socketio.Server.save_session`109 method is that when the ``namespace`` argument is not given the110 namespace associated with the class is used.111 """112 return self.server.save_session(113 sid, session, namespace=namespace or self.namespace)114 115 def session(self, sid, namespace=None):116 """Return the user session for a client with context manager syntax.117 118 The only difference with the :func:`socketio.Server.session` method is119 that when the ``namespace`` argument is not given the namespace120 associated with the class is used.121 """122 return self.server.session(sid, namespace=namespace or self.namespace)123 124 def disconnect(self, sid, namespace=None):125 """Disconnect a client.126 127 The only difference with the :func:`socketio.Server.disconnect` method128 is that when the ``namespace`` argument is not given the namespace129 associated with the class is used.130 """131 return self.server.disconnect(sid,132 namespace=namespace or self.namespace)133 134 135class ClientNamespace(base_namespace.BaseClientNamespace):136 """Base class for client-side class-based namespaces.137 138 A class-based namespace is a class that contains all the event handlers139 for a Socket.IO namespace. The event handlers are methods of the class140 with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``,141 ``on_message``, ``on_json``, and so on.142 143 :param namespace: The Socket.IO namespace to be used with all the event144 handlers defined in this class. If this argument is145 omitted, the default namespace is used.146 """147 def trigger_event(self, event, *args):148 """Dispatch an event to the proper handler method.149 150 In the most common usage, this method is not overloaded by subclasses,151 as it performs the routing of events to methods. However, this152 method can be overridden if special dispatching rules are needed, or if153 having a single method that catches all events is desired.154 """155 handler_name = 'on_' + event156 if hasattr(self, handler_name):157 return getattr(self, handler_name)(*args)158 159 def emit(self, event, data=None, namespace=None, callback=None):160 """Emit a custom event to the server.161 162 The only difference with the :func:`socketio.Client.emit` method is163 that when the ``namespace`` argument is not given the namespace164 associated with the class is used.165 """166 return self.client.emit(event, data=data,167 namespace=namespace or self.namespace,168 callback=callback)169 170 def send(self, data, room=None, namespace=None, callback=None):171 """Send a message to the server.172 173 The only difference with the :func:`socketio.Client.send` method is174 that when the ``namespace`` argument is not given the namespace175 associated with the class is used.176 """177 return self.client.send(data, namespace=namespace or self.namespace,178 callback=callback)179 180 def call(self, event, data=None, namespace=None, timeout=None):181 """Emit a custom event to the server and wait for the response.182 183 The only difference with the :func:`socketio.Client.call` method is184 that when the ``namespace`` argument is not given the namespace185 associated with the class is used.186 """187 return self.client.call(event, data=data,188 namespace=namespace or self.namespace,189 timeout=timeout)190 191 def disconnect(self):192 """Disconnect from the server.193 194 The only difference with the :func:`socketio.Client.disconnect` method195 is that when the ``namespace`` argument is not given the namespace196 associated with the class is used.197 """198 return self.client.disconnect()199 