Skip to content

Commit bd8555d

Browse files
Pass a reason argument to the disconnect handler (#1422)
1 parent 0b5c463 commit bd8555d

38 files changed

+469
-193
lines changed

docs/client.rst

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ server::
312312
print("The connection failed!")
313313

314314
@sio.event
315-
def disconnect():
316-
print("I'm disconnected!")
315+
def disconnect(reason):
316+
print("I'm disconnected! reason:", reason)
317317

318318
The ``connect_error`` handler is invoked when a connection attempt fails. If
319319
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
325325
networking failures. In the case of an accidental disconnection, the client is
326326
going to attempt to reconnect immediately after invoking the disconnect
327327
handler. As soon as the connection is re-established the connect handler will
328-
be invoked once again.
328+
be invoked once again. The handler receives a ``reason`` argument which
329+
provides the cause of the disconnection::
330+
331+
@sio.event
332+
def disconnect(reason):
333+
if reason == sio.reason.CLIENT_DISCONNECT:
334+
print('the client disconnected')
335+
elif reason == sio.reason.SERVER_DISCONNECT:
336+
print('the server disconnected the client')
337+
else:
338+
print('disconnect reason:', reason)
339+
340+
See the The :attr:`socketio.Client.reason` attribute for a list of possible
341+
disconnection reasons.
329342

330343
The ``connect``, ``connect_error`` and ``disconnect`` events have to be
331344
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
509522
def on_connect(self):
510523
pass
511524

512-
def on_disconnect(self):
525+
def on_disconnect(self, reason):
513526
pass
514527

515528
def on_my_event(self, data):
@@ -525,7 +538,7 @@ coroutines if desired::
525538
def on_connect(self):
526539
pass
527540

528-
def on_disconnect(self):
541+
def on_disconnect(self, reason):
529542
pass
530543

531544
async def on_my_event(self, data):

docs/server.rst

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ automatically when a client connects or disconnects from the server::
232232
print('connect ', sid)
233233

234234
@sio.event
235-
def disconnect(sid):
236-
print('disconnect ', sid)
235+
def disconnect(sid, reason):
236+
print('disconnect ', sid, reason)
237237

238238
The ``connect`` event is an ideal place to perform user authentication, and
239239
any necessary mapping between user entities in the application and the ``sid``
@@ -256,6 +256,21 @@ message::
256256
def connect(sid, environ, auth):
257257
raise ConnectionRefusedError('authentication failed')
258258

259+
The disconnect handler receives the ``sid`` assigned to the client and a
260+
``reason``, which provides the cause of the disconnection::
261+
262+
@sio.event
263+
def disconnect(sid, reason):
264+
if reason == sio.reason.CLIENT_DISCONNECT:
265+
print('the client disconnected')
266+
elif reason == sio.reason.SERVER_DISCONNECT:
267+
print('the server disconnected the client')
268+
else:
269+
print('disconnect reason:', reason)
270+
271+
See the The :attr:`socketio.Server.reason` attribute for a list of possible
272+
disconnection reasons.
273+
259274
Catch-All Event Handlers
260275
~~~~~~~~~~~~~~~~~~~~~~~~
261276

@@ -433,7 +448,7 @@ belong to a namespace can be created as methods in a subclass of
433448
def on_connect(self, sid, environ):
434449
pass
435450

436-
def on_disconnect(self, sid):
451+
def on_disconnect(self, sid, reason):
437452
pass
438453

439454
def on_my_event(self, sid, data):
@@ -449,7 +464,7 @@ if desired::
449464
def on_connect(self, sid, environ):
450465
pass
451466

452-
def on_disconnect(self, sid):
467+
def on_disconnect(self, sid, reason):
453468
pass
454469

455470
async def on_my_event(self, sid, data):

examples/client/async/fiddle_client.py

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

1111

1212
@sio.event
13-
async def disconnect():
14-
print('disconnected from server')
13+
async def disconnect(reason):
14+
print('disconnected from server, reason:', reason)
1515

1616

1717
@sio.event

examples/client/sync/fiddle_client.py

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

1010

1111
@sio.event
12-
def disconnect():
13-
print('disconnected from server')
12+
def disconnect(reason):
13+
print('disconnected from server, reason:', reason)
1414

1515

1616
@sio.event

examples/server/aiohttp/app.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
socket.on('connect', function() {
1212
socket.emit('my_event', {data: 'I\'m connected!'});
1313
});
14-
socket.on('disconnect', function() {
15-
$('#log').append('<br>Disconnected');
14+
socket.on('disconnect', function(reason) {
15+
$('#log').append('<br>Disconnected: ' + reason);
1616
});
1717
socket.on('my_response', function(msg) {
1818
$('#log').append('<br>Received: ' + msg.data);

examples/server/aiohttp/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ async def connect(sid, environ):
7070

7171

7272
@sio.event
73-
def disconnect(sid):
74-
print('Client disconnected')
73+
def disconnect(sid, reason):
74+
print('Client disconnected, reason:', reason)
7575

7676

7777
app.router.add_static('/static', 'static')
@@ -84,4 +84,4 @@ async def init_app():
8484

8585

8686
if __name__ == '__main__':
87-
web.run_app(init_app())
87+
web.run_app(init_app(), port=5000)

examples/server/aiohttp/fiddle.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ async def connect(sid, environ, auth):
1919

2020

2121
@sio.event
22-
def disconnect(sid):
23-
print('disconnected', sid)
22+
def disconnect(sid, reason):
23+
print('disconnected', sid, reason)
2424

2525

2626
app.router.add_static('/static', 'static')
2727
app.router.add_get('/', index)
2828

2929

3030
if __name__ == '__main__':
31-
web.run_app(app)
31+
web.run_app(app, port=5000)

examples/server/asgi/app.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
socket.on('connect', function() {
1212
socket.emit('my_event', {data: 'I\'m connected!'});
1313
});
14-
socket.on('disconnect', function() {
15-
$('#log').append('<br>Disconnected');
14+
socket.on('disconnect', function(reason) {
15+
$('#log').append('<br>Disconnected: ' + reason);
1616
});
1717
socket.on('my_response', function(msg) {
1818
$('#log').append('<br>Received: ' + msg.data);

examples/server/asgi/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ async def test_connect(sid, environ):
8888

8989

9090
@sio.on('disconnect')
91-
def test_disconnect(sid):
92-
print('Client disconnected')
91+
def test_disconnect(sid, reason):
92+
print('Client disconnected, reason:', reason)
9393

9494

9595
if __name__ == '__main__':

examples/server/asgi/fiddle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ async def connect(sid, environ, auth):
1717

1818

1919
@sio.event
20-
def disconnect(sid):
21-
print('disconnected', sid)
20+
def disconnect(sid, reason):
21+
print('disconnected', sid, reason)
2222

2323

2424
if __name__ == '__main__':

0 commit comments

Comments
 (0)