Skip to content

Commit 01628b7

Browse files
Add a reason argument to the disconnect handler
1 parent c3667e8 commit 01628b7

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

docs/getting_started.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ following example shows how to register handlers for them::
267267
emit('my response', {'data': 'Connected'})
268268

269269
@socketio.on('disconnect')
270-
def test_disconnect():
271-
print('Client disconnected')
270+
def test_disconnect(reason):
271+
print('Client disconnected, reason:', reason)
272272

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

291+
The disconnection event handler receives a ``reason`` argument that indicates
292+
the cause of the disconnection. The :attr:`flask_socketio.SocketIO.reason`
293+
member includes constants for all the possible reasons.
294+
291295
Note that connection and disconnection events are sent individually on each
292296
namespace used.
293297

@@ -305,7 +309,7 @@ create class-based namespaces::
305309
def on_connect(self):
306310
pass
307311

308-
def on_disconnect(self):
312+
def on_disconnect(self, reason):
309313
pass
310314

311315
def on_my_event(self, data):

example/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def connect():
118118

119119

120120
@socketio.on('disconnect')
121-
def test_disconnect():
122-
print('Client disconnected', request.sid)
121+
def test_disconnect(reason):
122+
print('Client disconnected', request.sid, reason)
123123

124124

125125
if __name__ == '__main__':

src/flask_socketio/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class SocketIO:
165165
fatal errors are logged even when
166166
``engineio_logger`` is ``False``.
167167
"""
168+
reason = socketio.Server.reason
168169

169170
def __init__(self, app=None, **kwargs):
170171
self.server = None

src/flask_socketio/namespace.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ def trigger_event(self, event, *args):
2222
# there is no handler for this event, so we ignore it
2323
return
2424
handler = getattr(self, handler_name)
25-
return self.socketio._handle_event(handler, event, self.namespace,
26-
*args)
25+
try:
26+
return self.socketio._handle_event(handler, event, self.namespace,
27+
*args)
28+
except TypeError:
29+
if event == 'disconnect':
30+
# legacy disconnect events do not have the reason argument
31+
return self.socketio._handle_event(
32+
handler, event, self.namespace, *args[:-1])
33+
else:
34+
raise
2735

2836
def emit(self, event, data=None, room=None, include_self=True,
2937
namespace=None, callback=None):

test_socketio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def on_connect(auth):
2626

2727

2828
@socketio.on('disconnect')
29-
def on_disconnect():
29+
def on_disconnect(reason):
3030
global disconnected
3131
disconnected = '/'
3232

0 commit comments

Comments
 (0)