Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
async_namespace.py256 linesDownload Raw Back to socketio
1import asyncio2 3from socketio import base_namespace4 5 6class AsyncNamespace(base_namespace.BaseServerNamespace):7    """Base class for asyncio server-side class-based namespaces.8 9    A class-based namespace is a class that contains all the event handlers10    for a Socket.IO namespace. The event handlers are methods of the class11    with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``,12    ``on_message``, ``on_json``, and so on. These can be regular functions or13    coroutines.14 15    :param namespace: The Socket.IO namespace to be used with all the event16                      handlers defined in this class. If this argument is17                      omitted, the default namespace is used.18    """19    def is_asyncio_based(self):20        return True21 22    async def trigger_event(self, event, *args):23        """Dispatch an event to the proper handler method.24 25        In the most common usage, this method is not overloaded by subclasses,26        as it performs the routing of events to methods. However, this27        method can be overridden if special dispatching rules are needed, or if28        having a single method that catches all events is desired.29 30        Note: this method is a coroutine.31        """32        handler_name = 'on_' + event33        if hasattr(self, handler_name):34            handler = getattr(self, handler_name)35            if asyncio.iscoroutinefunction(handler) is True:36                try:37                    ret = await handler(*args)38                except asyncio.CancelledError:  # pragma: no cover39                    ret = None40            else:41                ret = handler(*args)42            return ret43 44    async def emit(self, event, data=None, to=None, room=None, skip_sid=None,45                   namespace=None, callback=None, ignore_queue=False):46        """Emit a custom event to one or more connected clients.47 48        The only difference with the :func:`socketio.Server.emit` method is49        that when the ``namespace`` argument is not given the namespace50        associated with the class is used.51 52        Note: this method is a coroutine.53        """54        return await self.server.emit(event, data=data, to=to, room=room,55                                      skip_sid=skip_sid,56                                      namespace=namespace or self.namespace,57                                      callback=callback,58                                      ignore_queue=ignore_queue)59 60    async def send(self, data, to=None, room=None, skip_sid=None,61                   namespace=None, callback=None, ignore_queue=False):62        """Send a message to one or more connected clients.63 64        The only difference with the :func:`socketio.Server.send` method is65        that when the ``namespace`` argument is not given the namespace66        associated with the class is used.67 68        Note: this method is a coroutine.69        """70        return await self.server.send(data, to=to, room=room,71                                      skip_sid=skip_sid,72                                      namespace=namespace or self.namespace,73                                      callback=callback,74                                      ignore_queue=ignore_queue)75 76    async def call(self, event, data=None, to=None, sid=None, namespace=None,77                   timeout=None, ignore_queue=False):78        """Emit a custom event to a client and wait for the response.79 80        The only difference with the :func:`socketio.Server.call` method is81        that when the ``namespace`` argument is not given the namespace82        associated with the class is used.83        """84        return await self.server.call(event, data=data, to=to, sid=sid,85                                      namespace=namespace or self.namespace,86                                      timeout=timeout,87                                      ignore_queue=ignore_queue)88 89    async def enter_room(self, sid, room, namespace=None):90        """Enter a room.91 92        The only difference with the :func:`socketio.Server.enter_room` method93        is that when the ``namespace`` argument is not given the namespace94        associated with the class is used.95 96        Note: this method is a coroutine.97        """98        return await self.server.enter_room(99            sid, room, namespace=namespace or self.namespace)100 101    async def leave_room(self, sid, room, namespace=None):102        """Leave a room.103 104        The only difference with the :func:`socketio.Server.leave_room` method105        is that when the ``namespace`` argument is not given the namespace106        associated with the class is used.107 108        Note: this method is a coroutine.109        """110        return await self.server.leave_room(111            sid, room, namespace=namespace or self.namespace)112 113    async def close_room(self, room, namespace=None):114        """Close a room.115 116        The only difference with the :func:`socketio.Server.close_room` method117        is that when the ``namespace`` argument is not given the namespace118        associated with the class is used.119 120        Note: this method is a coroutine.121        """122        return await self.server.close_room(123            room, namespace=namespace or self.namespace)124 125    async def get_session(self, sid, namespace=None):126        """Return the user session for a client.127 128        The only difference with the :func:`socketio.Server.get_session`129        method is that when the ``namespace`` argument is not given the130        namespace associated with the class is used.131 132        Note: this method is a coroutine.133        """134        return await self.server.get_session(135            sid, namespace=namespace or self.namespace)136 137    async def save_session(self, sid, session, namespace=None):138        """Store the user session for a client.139 140        The only difference with the :func:`socketio.Server.save_session`141        method is that when the ``namespace`` argument is not given the142        namespace associated with the class is used.143 144        Note: this method is a coroutine.145        """146        return await self.server.save_session(147            sid, session, namespace=namespace or self.namespace)148 149    def session(self, sid, namespace=None):150        """Return the user session for a client with context manager syntax.151 152        The only difference with the :func:`socketio.Server.session` method is153        that when the ``namespace`` argument is not given the namespace154        associated with the class is used.155        """156        return self.server.session(sid, namespace=namespace or self.namespace)157 158    async def disconnect(self, sid, namespace=None):159        """Disconnect a client.160 161        The only difference with the :func:`socketio.Server.disconnect` method162        is that when the ``namespace`` argument is not given the namespace163        associated with the class is used.164 165        Note: this method is a coroutine.166        """167        return await self.server.disconnect(168            sid, namespace=namespace or self.namespace)169 170 171class AsyncClientNamespace(base_namespace.BaseClientNamespace):172    """Base class for asyncio client-side class-based namespaces.173 174    A class-based namespace is a class that contains all the event handlers175    for a Socket.IO namespace. The event handlers are methods of the class176    with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``,177    ``on_message``, ``on_json``, and so on. These can be regular functions or178    coroutines.179 180    :param namespace: The Socket.IO namespace to be used with all the event181                      handlers defined in this class. If this argument is182                      omitted, the default namespace is used.183    """184    def is_asyncio_based(self):185        return True186 187    async def trigger_event(self, event, *args):188        """Dispatch an event to the proper handler method.189 190        In the most common usage, this method is not overloaded by subclasses,191        as it performs the routing of events to methods. However, this192        method can be overridden if special dispatching rules are needed, or if193        having a single method that catches all events is desired.194 195        Note: this method is a coroutine.196        """197        handler_name = 'on_' + event198        if hasattr(self, handler_name):199            handler = getattr(self, handler_name)200            if asyncio.iscoroutinefunction(handler) is True:201                try:202                    ret = await handler(*args)203                except asyncio.CancelledError:  # pragma: no cover204                    ret = None205            else:206                ret = handler(*args)207            return ret208 209    async def emit(self, event, data=None, namespace=None, callback=None):210        """Emit a custom event to the server.211 212        The only difference with the :func:`socketio.Client.emit` method is213        that when the ``namespace`` argument is not given the namespace214        associated with the class is used.215 216        Note: this method is a coroutine.217        """218        return await self.client.emit(event, data=data,219                                      namespace=namespace or self.namespace,220                                      callback=callback)221 222    async def send(self, data, namespace=None, callback=None):223        """Send a message to the server.224 225        The only difference with the :func:`socketio.Client.send` method is226        that when the ``namespace`` argument is not given the namespace227        associated with the class is used.228 229        Note: this method is a coroutine.230        """231        return await self.client.send(data,232                                      namespace=namespace or self.namespace,233                                      callback=callback)234 235    async def call(self, event, data=None, namespace=None, timeout=None):236        """Emit a custom event to the server and wait for the response.237 238        The only difference with the :func:`socketio.Client.call` method is239        that when the ``namespace`` argument is not given the namespace240        associated with the class is used.241        """242        return await self.client.call(event, data=data,243                                      namespace=namespace or self.namespace,244                                      timeout=timeout)245 246    async def disconnect(self):247        """Disconnect a client.248 249        The only difference with the :func:`socketio.Client.disconnect` method250        is that when the ``namespace`` argument is not given the namespace251        associated with the class is used.252 253        Note: this method is a coroutine.254        """255        return await self.client.disconnect()256 
codekingpro/portable-devtools · Team Ai