Skip to content

Commit

Permalink
Handle events with catch-all namespace (Fixes #2061)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed May 9, 2024
1 parent 279bba4 commit f4ebcc7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,13 @@ def handle_my_custom_event(json):
def decorator(handler):
@wraps(handler)
def _handler(sid, *args):
return self._handle_event(handler, message, namespace, sid,
nonlocal namespace
real_ns = namespace
if namespace == '*':
real_ns = sid
sid = args[0]
args = args[1:]
return self._handle_event(handler, message, real_ns, sid,
*args)

if self.server:
Expand Down
15 changes: 15 additions & 0 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ def on_changing_response():
return data


@socketio.on('wildcard', namespace='*')
def wildcard(data):
emit('my custom response', data)


@socketio.on_error()
def error_handler(value):
if isinstance(value, AssertionError):
Expand Down Expand Up @@ -416,6 +421,16 @@ def test_send_json_namespace(self):
self.assertEqual(len(received), 1)
self.assertEqual(received[0]['args']['a'], 'b')

def test_send_catch_all_namespace(self):
client = socketio.test_client(app, namespace='/test')
client.get_received('/test')
client.emit('wildcard', {'a': 'b'}, namespace='/test')
received = client.get_received('/test')
self.assertEqual(len(received), 1)
self.assertEqual(len(received[0]['args']), 1)
self.assertEqual(received[0]['name'], 'my custom response')
self.assertEqual(received[0]['args'][0]['a'], 'b')

def test_emit(self):
client = socketio.test_client(app, auth={'foo': 'bar'})
client.get_received()
Expand Down

0 comments on commit f4ebcc7

Please sign in to comment.