Skip to content

Commit

Permalink
Merge pull request #4 from noirbizarre/werkzeug-debug
Browse files Browse the repository at this point in the history
Werkzeug debugger and reloader support
  • Loading branch information
miguelgrinberg committed Feb 13, 2014
2 parents be1d748 + 2c3d883 commit 1cf51be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ The following code example shows how to add Flask-SocketIO to a Flask applicatio

from flask import Flask, render_template
from flask.ext.socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

if __name__ == '__main__':
socketio.run(app)

The ``init_app()`` style of initialization is also supported. Note the way the web server is started. The ``socketio.run()`` function encapsulates the start up of the gevent web server. When using this extension the Werkzeug server cannot be used.
The ``init_app()`` style of initialization is also supported. Note the way the web server is started. The ``socketio.run()`` function encapsulates the start up of the gevent web server.

Receiving Messages
------------------
Expand Down
1 change: 1 addition & 0 deletions example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask.ext.socketio import SocketIO, emit

app = Flask(__name__)
app.debug=True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

Expand Down
13 changes: 12 additions & 1 deletion flask_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
from socketio.namespace import BaseNamespace
from flask import request, session
from flask.ctx import RequestContext
from werkzeug.debug import DebuggedApplication
from werkzeug.serving import run_with_reloader

monkey.patch_all()


class SocketIOMiddleware(object):
def __init__(self, app, socket):
self.app = app
if app.debug:
app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True)
self.wsgi_app = app.wsgi_app
self.socket = socket

Expand Down Expand Up @@ -142,7 +146,14 @@ def run(self, app, host=None, port=None):
port = int(server_name.rsplit(':', 1)[1])
else:
port = 5000
SocketIOServer((host, port), app.wsgi_app, resource='socket.io').serve_forever()
if app.debug:
@run_with_reloader
def run_server():
server = SocketIOServer((host, port), app.wsgi_app, resource='socket.io')
server.serve_forever()
run_server()
else:
SocketIOServer((host, port), app.wsgi_app, resource='socket.io').serve_forever()


def emit(event, *args, **kwargs):
Expand Down

0 comments on commit 1cf51be

Please sign in to comment.