From c106820bb47a70fef54c2154635db41f7acf60cf Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 2 Feb 2021 22:52:38 +0000 Subject: [PATCH] Adding missing example of async client implementation to intro.rst --- docs/intro.rst | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/docs/intro.rst b/docs/intro.rst index a120ce2..6fcdd44 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -68,6 +68,47 @@ The example that follows shows a simple Python client: sio.connect('http://localhost:5000') sio.wait() +Below is a similar client, coded for ``asyncio`` (Python 3.5+ only): + +.. code:: python + + import asyncio + + import socketio + + + sio = socketio.AsyncClient() + + @sio.event + async def send_message(data): + print('Sending a message:', data) + await sio.emit('chat_message', data) + + @sio.on("reply") + async def received_message(data): + print('Received a message:', data) + + @sio.event + async def connect(): + print("I'm connected! My sid is:", sio.sid) + await send_message({"message": "hello!"}) + + @sio.event + def connect_error(): + print("The connection failed!") + + @sio.event + async def disconnect(): + print("I'm disconnected!") + await sio.disconnect() + + + if __name__ == '__main__': + loop = asyncio.get_event_loop() + + loop.run_until_complete(sio.connect('http://localhost:8080')) + loop.run_until_complete(sio.wait()) + Client Features ---------------