Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 18, 2024
1 parent 06393fb commit b876b32
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
23 changes: 18 additions & 5 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
23 changes: 19 additions & 4 deletions docs/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand All @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
1 change: 0 additions & 1 deletion src/socketio/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ async def shutdown(self):
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):
Expand Down
1 change: 0 additions & 1 deletion src/socketio/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def signal_handler(sig, frame): # pragma: no cover
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,
Expand Down

0 comments on commit b876b32

Please sign in to comment.