From b976ff304e045aa375ec3f9f1f8f17483b2d1934 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Mon, 31 Aug 2015 23:39:35 -0700 Subject: [PATCH] Added a port of the "latency" example from the official Javascript client --- examples/README.rst | 49 ++++++++++++++ examples/latency.py | 45 +++++++++++++ {example => examples}/requirements.txt | 0 example/app.py => examples/simple.py | 2 +- {example => examples}/static/engine.io.js | 0 examples/static/style.css | 4 ++ examples/templates/latency.html | 64 +++++++++++++++++++ .../templates/simple.html | 0 8 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 examples/README.rst create mode 100755 examples/latency.py rename {example => examples}/requirements.txt (100%) rename example/app.py => examples/simple.py (97%) rename {example => examples}/static/engine.io.js (100%) create mode 100644 examples/static/style.css create mode 100755 examples/templates/latency.html rename example/templates/index.html => examples/templates/simple.html (100%) diff --git a/examples/README.rst b/examples/README.rst new file mode 100644 index 00000000..7c89e27a --- /dev/null +++ b/examples/README.rst @@ -0,0 +1,49 @@ +Engine.IO Examples +================== + +This directory contains example Engine.IO applications. + +simple.py +--------- + +A basic application in which the client sends messages to the server and the +server responds. + +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 Engine.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 simple.py + +or:: + + $ python latency.py + +Near the top of the ``simple.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/examples/latency.py b/examples/latency.py new file mode 100755 index 00000000..e73c6673 --- /dev/null +++ b/examples/latency.py @@ -0,0 +1,45 @@ +from flask import Flask, render_template + +import engineio + +async_mode = 'threading' + +eio = engineio.Server(async_mode=async_mode) +app = Flask(__name__) +app.wsgi_app = engineio.Middleware(eio, app.wsgi_app) + + +@app.route('/') +def index(): + return render_template('latency.html') + + +@eio.on('message') +def message(sid, data): + eio.send(sid, 'pong', binary=False) + + +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/example/app.py b/examples/simple.py similarity index 97% rename from example/app.py rename to examples/simple.py index d380eb48..e567d84c 100755 --- a/example/app.py +++ b/examples/simple.py @@ -11,7 +11,7 @@ @app.route('/') def index(): - return render_template('index.html') + return render_template('simple.html') @eio.on('connect') diff --git a/example/static/engine.io.js b/examples/static/engine.io.js similarity index 100% rename from example/static/engine.io.js rename to examples/static/engine.io.js diff --git a/examples/static/style.css b/examples/static/style.css new file mode 100644 index 00000000..d20bcad9 --- /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/examples/templates/latency.html b/examples/templates/latency.html new file mode 100755 index 00000000..53191cdb --- /dev/null +++ b/examples/templates/latency.html @@ -0,0 +1,64 @@ + + + + EIO Latency + + + +

EIO Latency

+

(connecting)

+ + + + + + + + diff --git a/example/templates/index.html b/examples/templates/simple.html similarity index 100% rename from example/templates/index.html rename to examples/templates/simple.html