Skip to content

Commit b084b2c

Browse files
basic socketio client (no reconnection yet)
1 parent b2a5850 commit b084b2c

6 files changed

Lines changed: 564 additions & 23 deletions

File tree

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ API Reference
4242

4343
.. autoclass:: Namespace
4444
:members:
45+
:inherited-members:
4546

4647
``AsyncNamespace`` class
4748
------------------------

socketio/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import sys
22

3+
from .client import Client
34
from .base_manager import BaseManager
45
from .pubsub_manager import PubSubManager
56
from .kombu_manager import KombuManager
67
from .redis_manager import RedisManager
78
from .zmq_manager import ZmqManager
89
from .server import Server
9-
from .namespace import Namespace
10+
from .namespace import Namespace, ClientNamespace
1011
from .middleware import WSGIApp, Middleware
1112
from .tornado import get_tornado_handler
1213
if sys.version_info >= (3, 5): # pragma: no cover
14+
# from .asyncio_client import AsyncClient
1315
from .asyncio_server import AsyncServer
1416
from .asyncio_manager import AsyncManager
15-
from .asyncio_namespace import AsyncNamespace
17+
from .asyncio_namespace import AsyncNamespace, AsyncClientNamespace
1618
from .asyncio_redis_manager import AsyncRedisManager
1719
from .asgi import ASGIApp
1820
else: # pragma: no cover
@@ -23,9 +25,10 @@
2325

2426
__version__ = '2.1.2'
2527

26-
__all__ = ['__version__', 'Server', 'BaseManager', 'PubSubManager',
28+
__all__ = ['__version__', 'Client', 'Server', 'BaseManager', 'PubSubManager',
2729
'KombuManager', 'RedisManager', 'ZmqManager', 'Namespace',
28-
'WSGIApp', 'Middleware']
30+
'ClientNamespace', 'WSGIApp', 'Middleware']
2931
if AsyncServer is not None: # pragma: no cover
30-
__all__ += ['AsyncServer', 'AsyncNamespace', 'AsyncManager',
31-
'AsyncRedisManager', 'ASGIApp', 'get_tornado_handler']
32+
__all__ += ['AsyncServer', 'AsyncNamespace', 'AsyncClientNamespace',
33+
'AsyncManager', 'AsyncRedisManager', 'ASGIApp',
34+
'get_tornado_handler']

socketio/asyncio_namespace.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,55 @@ async def disconnect(self, sid, namespace=None):
9393
"""
9494
return await self.server.disconnect(
9595
sid, namespace=namespace or self.namespace)
96+
97+
98+
class AsyncClientNamespace(namespace.ClientNamespace):
99+
"""Base class for asyncio class-based namespaces.
100+
101+
A class-based namespace is a class that contains all the event handlers
102+
for a Socket.IO namespace. The event handlers are methods of the class
103+
with the prefix ``on_``, such as ``on_connect``, ``on_disconnect``,
104+
``on_message``, ``on_json``, and so on. These can be regular functions or
105+
coroutines.
106+
107+
:param namespace: The Socket.IO namespace to be used with all the event
108+
handlers defined in this class. If this argument is
109+
omitted, the default namespace is used.
110+
"""
111+
async def emit(self, event, data=None, namespace=None, callback=None):
112+
"""Emit a custom event to the server.
113+
114+
The only difference with the :func:`socketio.Client.emit` method is
115+
that when the ``namespace`` argument is not given the namespace
116+
associated with the class is used.
117+
118+
Note: this method is a coroutine.
119+
"""
120+
return await self.server.emit(event, data=data,
121+
namespace=namespace or self.namespace,
122+
callback=callback)
123+
124+
async def send(self, data, namespace=None, callback=None):
125+
"""Send a message to the server.
126+
127+
The only difference with the :func:`socketio.Client.send` method is
128+
that when the ``namespace`` argument is not given the namespace
129+
associated with the class is used.
130+
131+
Note: this method is a coroutine.
132+
"""
133+
return await self.server.send(data,
134+
namespace=namespace or self.namespace,
135+
callback=callback)
136+
137+
async def disconnect(self, namespace=None):
138+
"""Disconnect a client.
139+
140+
The only difference with the :func:`socketio.Client.disconnect` method
141+
is that when the ``namespace`` argument is not given the namespace
142+
associated with the class is used.
143+
144+
Note: this method is a coroutine.
145+
"""
146+
return await self.server.disconnect(
147+
namespace=namespace or self.namespace)

0 commit comments

Comments
 (0)