Skip to content

Commit

Permalink
Add on_event(), the non-decorator version of on()
Browse files Browse the repository at this point in the history
  • Loading branch information
sotte authored and miguelgrinberg committed Aug 22, 2016
1 parent 6077d7a commit 39f8795
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ multiplex several independent connections on the same physical socket::
When a namespace is not specified a default global namespace with the name
``'/'`` is used.

For cases when a decorator syntax isn't convenient, the ``on_event`` method
can be used::

def my_function_handler(data):
pass

socketio.on_event('my event', my_function_handler, namespace='/test')

Clients may request an acknowledgement callback that confirms receipt of a
message. Any values returned from the handler function will be passed to the
client as arguments in the callback function::
Expand Down
25 changes: 25 additions & 0 deletions flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ def error_handler(e):
self.default_exception_handler = exception_handler
return exception_handler

def on_event(self, message, handler, namespace=None):
"""Register a SocketIO event handler.
``on_event`` is the non-decorator version of ``'on'``.
Example::
def on_foo_event(json):
print('received json: ' + str(json))
socketio.on_event('my event', on_foo_event, namespace='/chat')
:param message: The name of the event. This is normally a user defined
string, but a few event names are already defined. Use
``'message'`` to define a handler that takes a string
payload, ``'json'`` to define a handler that takes a
JSON blob payload, ``'connect'`` or ``'disconnect'``
to create handlers for connection and disconnection
events.
:param handler: The function that handles the event.
:param namespace: The namespace on which the handler is to be
registered. Defaults to the global namespace.
"""
self.on(message, namespace=namespace)(handler)

def emit(self, event, *args, **kwargs):
"""Emit a server generated SocketIO event.
Expand Down
36 changes: 36 additions & 0 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,26 @@ def get_request_event(data):
emit('my custom response', data)


def get_request_event2(data):
global request_event_data
request_event_data = request.event
emit('my custom response', data)

socketio.on_event('yet another custom event', get_request_event2)


@socketio.on('my custom namespace event', namespace='/test')
def on_custom_event_test(data):
emit('my custom namespace response', data, namespace='/test')


def on_custom_event_test2(data):
emit('my custom namespace response', data, namespace='/test')

socketio.on_event('yet another custom namespace event', on_custom_event_test2,
namespace='/test')


@socketio.on('my custom broadcast event')
def on_custom_event_broadcast(data):
emit('my custom response', data, broadcast=True)
Expand Down Expand Up @@ -425,5 +440,26 @@ def test_error_handling_ack(self):
callback=True)
self.assertIsNotNone(errorack_default)

def test_on_event(self):
client = socketio.test_client(app)
client.get_received()
global request_event_data
request_event_data = None
client.emit('yet another custom event', 'foo')
expected_data = {'message': 'yet another custom event',
'args': ('foo',)}
self.assertEqual(request_event_data, expected_data)

client = socketio.test_client(app, namespace='/test')
client.get_received('/test')
client.emit('yet another custom namespace event', {'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 namespace response')
self.assertEqual(received[0]['args'][0]['a'], 'b')


if __name__ == '__main__':
unittest.main()

0 comments on commit 39f8795

Please sign in to comment.