Skip to content

Commit

Permalink
Add a reason argument to the disconnect handler
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 18, 2024
1 parent c3667e8 commit 01628b7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
10 changes: 7 additions & 3 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ following example shows how to register handlers for them::
emit('my response', {'data': 'Connected'})

@socketio.on('disconnect')
def test_disconnect():
print('Client disconnected')
def test_disconnect(reason):
print('Client disconnected, reason:', reason)

The ``auth`` argument in the connection handler is optional. The client can
use it to pass authentication data such as tokens in dictionary format. If the
Expand All @@ -288,6 +288,10 @@ the exception are returned to the client in the error packet. Examples::
if not self.authenticate(request.args):
raise ConnectionRefusedError('unauthorized!')

The disconnection event handler receives a ``reason`` argument that indicates
the cause of the disconnection. The :attr:`flask_socketio.SocketIO.reason`
member includes constants for all the possible reasons.

Note that connection and disconnection events are sent individually on each
namespace used.

Expand All @@ -305,7 +309,7 @@ create class-based namespaces::
def on_connect(self):
pass

def on_disconnect(self):
def on_disconnect(self, reason):
pass

def on_my_event(self, data):
Expand Down
4 changes: 2 additions & 2 deletions example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def connect():


@socketio.on('disconnect')
def test_disconnect():
print('Client disconnected', request.sid)
def test_disconnect(reason):
print('Client disconnected', request.sid, reason)


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions src/flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class SocketIO:
fatal errors are logged even when
``engineio_logger`` is ``False``.
"""
reason = socketio.Server.reason

def __init__(self, app=None, **kwargs):
self.server = None
Expand Down
12 changes: 10 additions & 2 deletions src/flask_socketio/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ def trigger_event(self, event, *args):
# there is no handler for this event, so we ignore it
return
handler = getattr(self, handler_name)
return self.socketio._handle_event(handler, event, self.namespace,
*args)
try:
return self.socketio._handle_event(handler, event, self.namespace,
*args)
except TypeError:
if event == 'disconnect':
# legacy disconnect events do not have the reason argument
return self.socketio._handle_event(
handler, event, self.namespace, *args[:-1])
else:
raise

def emit(self, event, data=None, room=None, include_self=True,
namespace=None, callback=None):
Expand Down
2 changes: 1 addition & 1 deletion test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def on_connect(auth):


@socketio.on('disconnect')
def on_disconnect():
def on_disconnect(reason):
global disconnected
disconnected = '/'

Expand Down

0 comments on commit 01628b7

Please sign in to comment.