diff --git a/examples/README.rst b/examples/README.rst new file mode 100644 index 0000000..a67563e --- /dev/null +++ b/examples/README.rst @@ -0,0 +1,49 @@ +Engine.IO Examples +================== + +This directory contains example Engine.IO applications. + +app.py +------ + +A basic "kitchen sink" type application that allows the user to experiment +with most of the available features of the server. + +latency.py +---------- + +A port of the latency application included in the official Engine.IO +Javascript server. In this application the client sends *ping* messages to +the server, which are responded by the server with a *pong*. The client +measures the time it takes for each of these exchanges and plots these in real +time to the page. + +This is an ideal application to measure the performance of the different +asynchronous modes supported by the Socket.IO server. + +Running the Examples +-------------------- + +To run these examples using the default ``'threading'`` mode, create a virtual +environment, install the requirements and then run:: + + $ python app.py + +or:: + + $ python latency.py + +Near the top of the ``app.py`` and ``latency.py`` source files there is a +``async_mode`` variable that can be edited to swich to the other asynchornous +modes. Accepted values for ``async_mode`` are ``'threading'``, ``'eventlet'`` +and ``'gevent'``. + +Note 1: when using the ``'eventlet'`` mode, the eventlet package must be +installed in the virtual environment:: + + $ pip install eventlet + +Note 2: when using the ``'gevent'`` mode, the gevent and gevent-websocket +packages must be installed in the virtual environment:: + + $ pip install gevent gevent-websocket diff --git a/example/app.py b/examples/app.py similarity index 100% rename from example/app.py rename to examples/app.py diff --git a/examples/latency.py b/examples/latency.py new file mode 100755 index 0000000..191a069 --- /dev/null +++ b/examples/latency.py @@ -0,0 +1,46 @@ +from flask import Flask, render_template + +import socketio + +# set this to 'threading', 'eventlet', or 'gevent' +async_mode = 'eventlet' + +sio = socketio.Server(async_mode=async_mode) +app = Flask(__name__) +app.wsgi_app = socketio.Middleware(sio, app.wsgi_app) + + +@app.route('/') +def index(): + return render_template('latency.html') + + +@sio.on('ping') +def ping(sid): + sio.emit('pong', room=sid) + + +if __name__ == '__main__': + if async_mode == 'threading': + # deploy with Werkzeug + app.run(threaded=True) + elif async_mode == 'eventlet': + # deploy with eventlet + import eventlet + from eventlet import wsgi + wsgi.server(eventlet.listen(('', 5000)), app) + elif async_mode == 'gevent': + # deploy with gevent + from gevent import pywsgi + try: + from geventwebsocket.handler import WebSocketHandler + websocket = True + except ImportError: + websocket = False + if websocket: + pywsgi.WSGIServer(('', 5000), app, + handler_class=WebSocketHandler).serve_forever() + else: + pywsgi.WSGIServer(('', 5000), app).serve_forever() + else: + print('Unknown async_mode: ' + async_mode) diff --git a/example/requirements.txt b/examples/requirements.txt similarity index 100% rename from example/requirements.txt rename to examples/requirements.txt diff --git a/examples/static/style.css b/examples/static/style.css new file mode 100644 index 0000000..d20bcad --- /dev/null +++ b/examples/static/style.css @@ -0,0 +1,4 @@ +body { margin: 0; padding: 0; font-family: Helvetica Neue; } +h1 { margin: 100px 100px 10px; } +h2 { color: #999; margin: 0 100px 30px; font-weight: normal; } +#latency { color: red; } diff --git a/example/templates/index.html b/examples/templates/index.html old mode 100644 new mode 100755 similarity index 99% rename from example/templates/index.html rename to examples/templates/index.html index 402bf40..1668814 --- a/example/templates/index.html +++ b/examples/templates/index.html @@ -2,7 +2,7 @@