Skip to content

Commit 34f34e5

Browse files
Do not invoke reserved events on a catch-all handler (Fixes #814)
1 parent 60735dd commit 34f34e5

File tree

8 files changed

+24
-8
lines changed

8 files changed

+24
-8
lines changed

docs/client.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,23 @@ or can also be coroutines::
6565
async def message(data):
6666
print('I received a message!')
6767

68+
If the server includes arguments with an event, those are passed to the
69+
handler function as arguments.
70+
6871
Catch-All Event Handlers
6972
------------------------
7073

7174
A "catch-all" event handler is invoked for any events that do not have an
7275
event handler. You can define a catch-all handler using ``'*'`` as event name::
7376

7477
@sio.on('*')
75-
def catch_all(event, sid, data):
78+
def catch_all(event, data):
7679
pass
7780

7881
Asyncio clients can also use a coroutine::
7982

8083
@sio.on('*')
81-
async def catch_all(event, sid, data):
84+
async def catch_all(event, data):
8285
pass
8386

8487
A catch-all event handler receives the event name as a first argument. The
@@ -115,8 +118,8 @@ going to attempt to reconnect immediately after invoking the disconnect
115118
handler. As soon as the connection is re-established the connect handler will
116119
be invoked once again.
117120

118-
If the server includes arguments with an event, those are passed to the
119-
handler function as arguments.
121+
The ``connect``, ``connect_error`` and ``disconnect`` events have to be
122+
defined explicitly and are not invoked on a catch-all event handler.
120123

121124
Connecting to a Server
122125
----------------------

docs/server.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ Asyncio servers can also use a coroutine::
197197
A catch-all event handler receives the event name as a first argument. The
198198
remaining arguments are the same as for a regular event handler.
199199

200+
The ``connect`` and ``disconnect`` events have to be defined explicitly and are
201+
not invoked on a catch-all event handler.
202+
200203
Connect and Disconnect Event Handlers
201204
-------------------------------------
202205

src/socketio/asyncio_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ async def _trigger_event(self, event, namespace, *args):
431431
handler = None
432432
if event in self.handlers[namespace]:
433433
handler = self.handlers[namespace][event]
434-
elif '*' in self.handlers[namespace]:
434+
elif event not in self.reserved_events and \
435+
'*' in self.handlers[namespace]:
435436
handler = self.handlers[namespace]['*']
436437
args = (event, *args)
437438
if handler:

src/socketio/asyncio_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,8 @@ async def _trigger_event(self, event, namespace, *args):
532532
handler = None
533533
if event in self.handlers[namespace]:
534534
handler = self.handlers[namespace][event]
535-
elif '*' in self.handlers[namespace]:
535+
elif event not in self.reserved_events and \
536+
'*' in self.handlers[namespace]:
536537
handler = self.handlers[namespace]['*']
537538
args = (event, *args)
538539
if handler:

src/socketio/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class Client(object):
9292
fatal errors are logged even when
9393
``engineio_logger`` is ``False``.
9494
"""
95+
reserved_events = ['connect', 'connect_error', 'disconnect']
96+
9597
def __init__(self, reconnection=True, reconnection_attempts=0,
9698
reconnection_delay=1, reconnection_delay_max=5,
9799
randomization_factor=0.5, logger=False, serializer='default',
@@ -625,7 +627,8 @@ def _trigger_event(self, event, namespace, *args):
625627
if namespace in self.handlers:
626628
if event in self.handlers[namespace]:
627629
return self.handlers[namespace][event](*args)
628-
elif '*' in self.handlers[namespace]:
630+
elif event not in self.reserved_events and \
631+
'*' in self.handlers[namespace]:
629632
return self.handlers[namespace]['*'](event, *args)
630633

631634
# or else, forward the event to a namespace handler if one exists

src/socketio/server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class Server(object):
106106
fatal errors are logged even when
107107
``engineio_logger`` is ``False``.
108108
"""
109+
reserved_events = ['connect', 'disconnect']
110+
109111
def __init__(self, client_manager=None, logger=False, serializer='default',
110112
json=None, async_handlers=True, always_connect=False,
111113
**kwargs):
@@ -741,7 +743,8 @@ def _trigger_event(self, event, namespace, *args):
741743
if namespace in self.handlers:
742744
if event in self.handlers[namespace]:
743745
return self.handlers[namespace][event](*args)
744-
elif '*' in self.handlers[namespace]:
746+
elif event not in self.reserved_events and \
747+
'*' in self.handlers[namespace]:
745748
return self.handlers[namespace]['*'](event, *args)
746749

747750
# or else, forward the event to a namespace handler if one exists

tests/asyncio/test_asyncio_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ def test_trigger_event(self):
838838
c.on('*', catchall_handler)
839839
_run(c._trigger_event('foo', '/', 1, '2'))
840840
_run(c._trigger_event('bar', '/', 1, '2', 3))
841+
_run(c._trigger_event('connect', '/')) # should not trigger
841842
handler.assert_called_once_with(1, '2')
842843
catchall_handler.assert_called_once_with('bar', 1, '2', 3)
843844

tests/common/test_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ def test_trigger_event(self):
943943
c.on('*', catchall_handler)
944944
c._trigger_event('foo', '/', 1, '2')
945945
c._trigger_event('bar', '/', 1, '2', 3)
946+
c._trigger_event('connect', '/') # should not trigger
946947
handler.assert_called_once_with(1, '2')
947948
catchall_handler.assert_called_once_with('bar', 1, '2', 3)
948949

0 commit comments

Comments
 (0)