Skip to content

Commit

Permalink
Support catch all events (Fixes #2095)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Sep 29, 2024
1 parent c4f0218 commit 45990f6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
9 changes: 8 additions & 1 deletion example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
socketio = SocketIO(app, async_mode=async_mode, logger=True, engineio_logger=True)
thread = None
thread_lock = Lock()

Expand Down Expand Up @@ -81,6 +81,13 @@ def my_room_event(message):
to=message['room'])


@socketio.on('*')
def catch_all(event, data):
session['receive_count'] = session.get('receive_count', 0) + 1
emit('my_response',
{'data': [event, data], 'count': session['receive_count']})


@socketio.event
def disconnect_request():
@copy_current_request_context
Expand Down
4 changes: 2 additions & 2 deletions example/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>Flask-SocketIO Test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js" integrity="sha512-aMGMvNYu8Ue4G+fHa359jcPb1u+ytAF+P2SCb+PxrjCdO3n3ZTxJ30zuH39rimUggmTwmh2u7wvQsDTHESnmfQ==" crossorigin="anonymous"></script>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js" integrity="sha384-2huaZvOR9iDzHqslqwpR87isEmrfxqyWOF7hr7BY6KG0+hVKLoEXMPUJw3ynWuhO" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Connect to the Socket.IO server.
Expand Down Expand Up @@ -56,7 +56,7 @@
// These accept data from the user and send it to the server in a
// variety of ways
$('form#emit').submit(function(event) {
socket.emit('my_event', {data: $('#emit_data').val()});
socket.emit('my_event_foo', {data: $('#emit_data').val()});
return false;
});
$('form#broadcast').submit(function(event) {
Expand Down
6 changes: 6 additions & 0 deletions src/flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def _handler(sid, *args):
real_ns = sid
sid = args[0]
args = args[1:]
real_msg = message
if message == '*':
real_msg = sid
sid = args[0]
args = [real_msg] + list(args[1:])
print(f'{real_msg=} {real_ns=} {sid=} {args=}')
return self._handle_event(handler, message, real_ns, sid,
*args)

Expand Down
36 changes: 26 additions & 10 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def get_request_event2(data):
socketio.on_event('yet another custom event', get_request_event2)


@socketio.on('*')
def catch_all(event, data):
emit('my custom response', (event, data))


@socketio.on('my custom namespace event', namespace='/test')
def on_custom_event_test(data):
emit('my custom namespace response', data, namespace='/test')
Expand Down Expand Up @@ -421,16 +426,6 @@ 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 All @@ -451,6 +446,27 @@ def test_emit_binary(self):
self.assertEqual(received[0]['name'], 'my custom response')
self.assertEqual(received[0]['args'][0]['a'], b'\x01\x02\x03')

def test_emit_catch_all_event(self):
client = socketio.test_client(app, auth={'foo': 'bar'})
client.get_received()
client.emit('random event', {'foo': 'bar'})
received = client.get_received()
self.assertEqual(len(received), 1)
self.assertEqual(len(received[0]['args']), 2)
self.assertEqual(received[0]['name'], 'my custom response')
self.assertEqual(received[0]['args'][0], 'random event')
self.assertEqual(received[0]['args'][1], {'foo': 'bar'})

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_request_event_data(self):
client = socketio.test_client(app, auth={'foo': 'bar'})
client.get_received()
Expand Down

0 comments on commit 45990f6

Please sign in to comment.