From 4c5d6b4b492b89d67d4607d44e27ee39b3da5e26 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg <miguel.grinberg@gmail.com> Date: Wed, 18 Dec 2024 00:11:39 +0000 Subject: [PATCH] docs --- docs/client.rst | 23 ++++++++++++++++++----- docs/server.rst | 23 +++++++++++++++++++---- src/socketio/async_client.py | 1 - src/socketio/base_client.py | 1 - tests/async/test_manager.py | 2 -- tests/common/test_client.py | 1 - 6 files changed, 37 insertions(+), 14 deletions(-) diff --git a/docs/client.rst b/docs/client.rst index 1a55b71..e3e1fb2 100644 --- a/docs/client.rst +++ b/docs/client.rst @@ -312,8 +312,8 @@ server:: print("The connection failed!") @sio.event - def disconnect(): - print("I'm disconnected!") + def disconnect(reason): + print("I'm disconnected! reason:", reason) The ``connect_error`` handler is invoked when a connection attempt fails. If the server provides arguments, these are passed on to the handler. The server @@ -325,7 +325,20 @@ server initiated disconnects, or accidental disconnects, for example due to networking failures. In the case of an accidental disconnection, the client is going to attempt to reconnect immediately after invoking the disconnect handler. As soon as the connection is re-established the connect handler will -be invoked once again. +be invoked once again. The handler receives a ``reason`` argument which +provides the cause of the disconnection:: + + @sio.event + def disconnect(reason): + if reason == sio.reason.CLIENT_DISCONNECT: + print('the client disconnected') + elif reason == sio.reason.SERVER_DISCONNECT: + print('the server disconnected the client') + else: + print('disconnect reason:', reason) + +See the The :attr:`socketio.Client.reason` attribute for a list of possible +disconnection reasons. The ``connect``, ``connect_error`` and ``disconnect`` events have to be defined explicitly and are not invoked on a catch-all event handler. @@ -509,7 +522,7 @@ that belong to a namespace can be created as methods of a subclass of def on_connect(self): pass - def on_disconnect(self): + def on_disconnect(self, reason): pass def on_my_event(self, data): @@ -525,7 +538,7 @@ coroutines if desired:: def on_connect(self): pass - def on_disconnect(self): + def on_disconnect(self, reason): pass async def on_my_event(self, data): diff --git a/docs/server.rst b/docs/server.rst index c20adf9..ed15ed3 100644 --- a/docs/server.rst +++ b/docs/server.rst @@ -232,8 +232,8 @@ automatically when a client connects or disconnects from the server:: print('connect ', sid) @sio.event - def disconnect(sid): - print('disconnect ', sid) + def disconnect(sid, reason): + print('disconnect ', sid, reason) The ``connect`` event is an ideal place to perform user authentication, and any necessary mapping between user entities in the application and the ``sid`` @@ -256,6 +256,21 @@ message:: def connect(sid, environ, auth): raise ConnectionRefusedError('authentication failed') +The disconnect handler receives the ``sid`` assigned to the client and a +``reason``, which provides the cause of the disconnection:: + + @sio.event + def disconnect(sid, reason): + if reason == sio.reason.CLIENT_DISCONNECT: + print('the client disconnected') + elif reason == sio.reason.SERVER_DISCONNECT: + print('the server disconnected the client') + else: + print('disconnect reason:', reason) + +See the The :attr:`socketio.Server.reason` attribute for a list of possible +disconnection reasons. + Catch-All Event Handlers ~~~~~~~~~~~~~~~~~~~~~~~~ @@ -433,7 +448,7 @@ belong to a namespace can be created as methods in a subclass of def on_connect(self, sid, environ): pass - def on_disconnect(self, sid): + def on_disconnect(self, sid, reason): pass def on_my_event(self, sid, data): @@ -449,7 +464,7 @@ if desired:: def on_connect(self, sid, environ): pass - def on_disconnect(self, sid): + def on_disconnect(self, sid, reason): pass async def on_my_event(self, sid, data): diff --git a/src/socketio/async_client.py b/src/socketio/async_client.py index c3a42bd..463073e 100644 --- a/src/socketio/async_client.py +++ b/src/socketio/async_client.py @@ -338,7 +338,6 @@ class AsyncClient(base_client.BaseClient): await self.disconnect() elif self._reconnect_task: # pragma: no branch self._reconnect_abort.set() - print(self._reconnect_task) await self._reconnect_task def start_background_task(self, target, *args, **kwargs): diff --git a/src/socketio/base_client.py b/src/socketio/base_client.py index ef89c11..7bf4420 100644 --- a/src/socketio/base_client.py +++ b/src/socketio/base_client.py @@ -33,7 +33,6 @@ original_signal_handler = None class BaseClient: reserved_events = ['connect', 'connect_error', 'disconnect', '__disconnect_final'] - print(dir(engineio.Client)) reason = engineio.Client.reason def __init__(self, reconnection=True, reconnection_attempts=0, diff --git a/tests/async/test_manager.py b/tests/async/test_manager.py index fd5fe81..aa89064 100644 --- a/tests/async/test_manager.py +++ b/tests/async/test_manager.py @@ -183,8 +183,6 @@ class TestAsyncManager: await self.bm.enter_room(sid, '/foo', 'bar') await self.bm.enter_room(sid, '/foo', 'bar') await self.bm.close_room('bar', '/foo') - from pprint import pprint - pprint(self.bm.rooms) assert 'bar' not in self.bm.rooms['/foo'] async def test_close_invalid_room(self): diff --git a/tests/common/test_client.py b/tests/common/test_client.py index 1ebcea7..ac930c7 100644 --- a/tests/common/test_client.py +++ b/tests/common/test_client.py @@ -795,7 +795,6 @@ class TestClient: c.namespaces = {'/foo': '1', '/bar': '2'} c._trigger_event = mock.MagicMock() c._handle_disconnect('/') - print(c._trigger_event.call_args_list) c._trigger_event.assert_any_call('disconnect', '/', c.reason.SERVER_DISCONNECT) c._trigger_event.assert_any_call('__disconnect_final', '/')