Support catch all events (Fixes #2095)

This commit is contained in:
Miguel Grinberg 2024-09-29 20:45:06 +01:00
parent c4f0218c35
commit 45990f6721
No known key found for this signature in database
4 changed files with 42 additions and 13 deletions

View File

@ -11,7 +11,7 @@ async_mode = None
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()
@ -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

View File

@ -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.
@ -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) {

View File

@ -285,6 +285,12 @@ class SocketIO(object):
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)

View File

@ -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')
@ -421,16 +426,6 @@ class TestSocketIO(unittest.TestCase):
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()
@ -451,6 +446,27 @@ class TestSocketIO(unittest.TestCase):
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()