Skip to content

Commit

Permalink
Fix socket.on decorator when using delayed app initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 19, 2015
1 parent 88d592f commit 658945c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class SocketIO(object):
def __init__(self, app=None, **kwargs):
self.server = None
self.server_options = None
self.handlers = []
self.exception_handlers = {}
self.default_exception_handler = None
if app is not None:
Expand All @@ -101,6 +102,8 @@ def init_app(self, app, **kwargs):
if resource.startswith('/'):
resource = resource[1:]
self.server = socketio.Server(**self.server_options)
for handler in self.handlers:
self.server.on(handler[0], handler[1], namespace=handler[2])
app.wsgi_app = _SocketIOMiddleware(self.server, app,
socketio_path=resource)

Expand All @@ -117,8 +120,8 @@ def handle_my_custom_event(json):
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
JSON blob payload, ``'connect'`` or ``'disconnect'``
to create handlers for connection and disconnection
events.
:param namespace: The namespace on which the handler is to be
registered. Defaults to the global namespace.
Expand Down Expand Up @@ -154,7 +157,10 @@ def _handler(sid, *args):
flask.session,
self.server.environ[sid]['saved_session'])
return ret
self.server.on(message, _handler, namespace=namespace)
if self.server:
self.server.on(message, _handler, namespace=namespace)
else:
self.handlers.append((message, _handler, namespace))
return _handler
return decorator

Expand Down

0 comments on commit 658945c

Please sign in to comment.