Skip to content

Commit

Permalink
Merge pull request #101 from Romainpaulus/request-event
Browse files Browse the repository at this point in the history
Add event information in flask request variable
  • Loading branch information
miguelgrinberg committed Mar 13, 2015
2 parents 45a8236 + a78432a commit 63fc98f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ Flask-SocketIO can also deal with exceptions::

Error handler functions take the exception object as an argument.

The message and data arguments of the current request can also be inspected with the ``request.event`` variable, which is useful for error logging and debugging outside the event handler::

from flask import request

@socketio.on("my error event")
def on_my_event(data):
raise RuntimeError()

@socketio.on_error_default
def default_error_handler(e):
print request.event["message"] # "my error event"
print request.event["args"] # (data,)

Access to Flask's Context Globals
---------------------------------

Expand Down
3 changes: 3 additions & 0 deletions flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def _dispatch_message(self, app, namespace, message, args=[]):
return
with app.request_context(namespace.environ):
request.namespace = namespace
request.event = {
"message": message,
"args": args}
for k, v in namespace.session.items():
session[k] = v
ret = self.messages[namespace.ns_name][message](*args)
Expand Down
18 changes: 17 additions & 1 deletion test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
cov = coverage.coverage()
cov.start()

from flask import Flask, session
from flask import Flask, session, request
from flask.ext.socketio import SocketIO, send, emit, join_room, leave_room

app = Flask(__name__)
Expand Down Expand Up @@ -64,6 +64,13 @@ def on_custom_event(data):
emit('my custom response', data)


@socketio.on('other custom event')
def get_request_event(data):
global request_event_data
request_event_data = request.event
emit('my custom response', 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 @@ -245,6 +252,15 @@ def test_emit(self):
self.assertTrue(received[0]['name'] == 'my custom response')
self.assertTrue(received[0]['args'][0]['a'] == 'b')

def test_request_event_data(self):
client = socketio.test_client(app)
client.get_received() # clean received
global request_event_data
request_event_data = None
client.emit('other custom event', 'foo')
expected_data = {'message': 'other custom event', 'args' : ('foo',)}
self.assertTrue(request_event_data == expected_data)

def test_emit_namespace(self):
client = socketio.test_client(app, namespace='/test')
client.get_received('/test') # clean received
Expand Down

0 comments on commit 63fc98f

Please sign in to comment.