|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from socketio import namespace |
| 4 | + |
| 5 | + |
| 6 | +class AsyncNamespace(namespace.Namespace): |
| 7 | + """Base class for asyncio class-based namespaces. |
| 8 | +
|
| 9 | + A class-based namespace is a class that contains all the event handlers |
| 10 | + for a Socket.IO namespace. The event handlers are methods of the class |
| 11 | + with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``, |
| 12 | + ``on_message``, ``on_json``, and so on. These can be regular functions or |
| 13 | + coroutines. |
| 14 | +
|
| 15 | + :param namespace: The Socket.IO namespace to be used with all the event |
| 16 | + handlers defined in this class. If this argument is |
| 17 | + omitted, the default namespace is used. |
| 18 | + """ |
| 19 | + def is_asyncio_based(self): |
| 20 | + return True |
| 21 | + |
| 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, this |
| 27 | + method can be overriden if special dispatching rules are needed, or if |
| 28 | + having a single method that catches all events is desired. |
| 29 | +
|
| 30 | + Note: this method is a coroutine. |
| 31 | + """ |
| 32 | + handler_name = 'on_' + event |
| 33 | + 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 cover |
| 39 | + pass |
| 40 | + else: |
| 41 | + ret = handler(*args) |
| 42 | + return ret |
| 43 | + |
| 44 | + async def emit(self, event, data=None, room=None, skip_sid=None, |
| 45 | + namespace=None, callback=None): |
| 46 | + """Emit a custom event to one or more connected clients. |
| 47 | +
|
| 48 | + The only difference with the :func:`socketio.Server.emit` method is |
| 49 | + that when the ``namespace`` argument is not given the namespace |
| 50 | + associated with the class is used. |
| 51 | +
|
| 52 | + Note: this method is a coroutine. |
| 53 | + """ |
| 54 | + return await self.server.emit(event, data=data, room=room, |
| 55 | + skip_sid=skip_sid, |
| 56 | + namespace=namespace or self.namespace, |
| 57 | + callback=callback) |
| 58 | + |
| 59 | + async def send(self, data, room=None, skip_sid=None, namespace=None, |
| 60 | + callback=None): |
| 61 | + """Send a message to one or more connected clients. |
| 62 | +
|
| 63 | + The only difference with the :func:`socketio.Server.send` method is |
| 64 | + that when the ``namespace`` argument is not given the namespace |
| 65 | + associated with the class is used. |
| 66 | +
|
| 67 | + Note: this method is a coroutine. |
| 68 | + """ |
| 69 | + return await self.server.send(data, room=room, skip_sid=skip_sid, |
| 70 | + namespace=namespace or self.namespace, |
| 71 | + callback=callback) |
| 72 | + |
| 73 | + async def disconnect(self, sid, namespace=None): |
| 74 | + """Disconnect a client. |
| 75 | +
|
| 76 | + The only difference with the :func:`socketio.Server.disconnect` method |
| 77 | + is that when the ``namespace`` argument is not given the namespace |
| 78 | + associated with the class is used. |
| 79 | +
|
| 80 | + Note: this method is a coroutine. |
| 81 | + """ |
| 82 | + return await self.server.disconnect( |
| 83 | + sid, namespace=namespace or self.namespace) |
0 commit comments