Skip to content

Commit 798c126

Browse files
Added a latency check example
1 parent 1710080 commit 798c126

File tree

9 files changed

+166
-3
lines changed

9 files changed

+166
-3
lines changed

examples/README.rst

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Engine.IO Examples
2+
==================
3+
4+
This directory contains example Engine.IO applications.
5+
6+
app.py
7+
------
8+
9+
A basic "kitchen sink" type application that allows the user to experiment
10+
with most of the available features of the server.
11+
12+
latency.py
13+
----------
14+
15+
A port of the latency application included in the official Engine.IO
16+
Javascript server. In this application the client sends *ping* messages to
17+
the server, which are responded by the server with a *pong*. The client
18+
measures the time it takes for each of these exchanges and plots these in real
19+
time to the page.
20+
21+
This is an ideal application to measure the performance of the different
22+
asynchronous modes supported by the Socket.IO server.
23+
24+
Running the Examples
25+
--------------------
26+
27+
To run these examples using the default ``'threading'`` mode, create a virtual
28+
environment, install the requirements and then run::
29+
30+
$ python app.py
31+
32+
or::
33+
34+
$ python latency.py
35+
36+
Near the top of the ``app.py`` and ``latency.py`` source files there is a
37+
``async_mode`` variable that can be edited to swich to the other asynchornous
38+
modes. Accepted values for ``async_mode`` are ``'threading'``, ``'eventlet'``
39+
and ``'gevent'``.
40+
41+
Note 1: when using the ``'eventlet'`` mode, the eventlet package must be
42+
installed in the virtual environment::
43+
44+
$ pip install eventlet
45+
46+
Note 2: when using the ``'gevent'`` mode, the gevent and gevent-websocket
47+
packages must be installed in the virtual environment::
48+
49+
$ pip install gevent gevent-websocket
File renamed without changes.

examples/latency.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from flask import Flask, render_template
2+
3+
import socketio
4+
5+
# set this to 'threading', 'eventlet', or 'gevent'
6+
async_mode = 'eventlet'
7+
8+
sio = socketio.Server(async_mode=async_mode)
9+
app = Flask(__name__)
10+
app.wsgi_app = socketio.Middleware(sio, app.wsgi_app)
11+
12+
13+
@app.route('/')
14+
def index():
15+
return render_template('latency.html')
16+
17+
18+
@sio.on('ping')
19+
def ping(sid):
20+
sio.emit('pong', room=sid)
21+
22+
23+
if __name__ == '__main__':
24+
if async_mode == 'threading':
25+
# deploy with Werkzeug
26+
app.run(threaded=True)
27+
elif async_mode == 'eventlet':
28+
# deploy with eventlet
29+
import eventlet
30+
from eventlet import wsgi
31+
wsgi.server(eventlet.listen(('', 5000)), app)
32+
elif async_mode == 'gevent':
33+
# deploy with gevent
34+
from gevent import pywsgi
35+
try:
36+
from geventwebsocket.handler import WebSocketHandler
37+
websocket = True
38+
except ImportError:
39+
websocket = False
40+
if websocket:
41+
pywsgi.WSGIServer(('', 5000), app,
42+
handler_class=WebSocketHandler).serve_forever()
43+
else:
44+
pywsgi.WSGIServer(('', 5000), app).serve_forever()
45+
else:
46+
print('Unknown async_mode: ' + async_mode)
File renamed without changes.

examples/static/style.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body { margin: 0; padding: 0; font-family: Helvetica Neue; }
2+
h1 { margin: 100px 100px 10px; }
3+
h2 { color: #999; margin: 0 100px 30px; font-weight: normal; }
4+
#latency { color: red; }

example/templates/index.html renamed to examples/templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<title>Flask-SocketIO Test</title>
5-
<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>
5+
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
66
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
77
<script type="text/javascript" charset="utf-8">
88
$(document).ready(function(){

examples/templates/latency.html

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Socket.IO Latency</title>
5+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
6+
</head>
7+
<body>
8+
<h1>Socket.IO Latency <span id="latency"></span></h1>
9+
<h2 id="transport">(connecting)</h2>
10+
<canvas id="chart" height="200"></canvas>
11+
12+
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
13+
<script src="//cdnjs.cloudflare.com/ajax/libs/smoothie/1.27.0/smoothie.js"></script>
14+
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
15+
<script>
16+
// socket
17+
var socket = io.connect('http://' + document.domain + ':' + location.port);
18+
var char = $('chart').get(0);
19+
socket.on('connect', function() {
20+
if (chart.getContext) {
21+
render();
22+
window.onresize = render;
23+
}
24+
send();
25+
});
26+
socket.on('pong', function() {
27+
var latency = new Date - last;
28+
$('#latency').text(latency + 'ms');
29+
if (time)
30+
time.append(+new Date, latency);
31+
setTimeout(send, 100);
32+
});
33+
socket.on('disconnect', function() {
34+
if (smoothie)
35+
smoothie.stop();
36+
$('#transport').text('(disconnected)');
37+
});
38+
39+
var last;
40+
function send() {
41+
last = new Date;
42+
socket.emit('ping');
43+
$('#transport').text(socket.io.engine.transport.name);
44+
}
45+
46+
// chart
47+
var smoothie;
48+
var time;
49+
function render() {
50+
if (smoothie)
51+
smoothie.stop();
52+
chart.width = document.body.clientWidth;
53+
smoothie = new SmoothieChart();
54+
smoothie.streamTo(chart, 1000);
55+
time = new TimeSeries();
56+
smoothie.addTimeSeries(time, {
57+
strokeStyle: 'rgb(255, 0, 0)',
58+
fillStyle: 'rgba(255, 0, 0, 0.4)',
59+
lineWidth: 2
60+
});
61+
}
62+
</script>
63+
</body>
64+
</html>

socketio/server.py

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def set_handler(handler):
150150
return set_handler
151151
set_handler(handler)
152152

153-
def emit(self, event, data, room=None, skip_sid=None, namespace=None,
153+
def emit(self, event, data=None, room=None, skip_sid=None, namespace=None,
154154
callback=None):
155155
"""Emit a custom event to one or more connected clients.
156156

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ basepython=python
1313
deps=
1414
flake8
1515
commands=
16-
flake8 --exclude=".*" socketio tests example
16+
flake8 --exclude=".*" socketio tests
1717

1818
[testenv:py27]
1919
basepython=python2.7

0 commit comments

Comments
 (0)