-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2da228
commit 251485d
Showing
8 changed files
with
4,453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Engine.IO Examples | ||
================== | ||
|
||
This directory contains example Engine.IO applications that are compatible | ||
with asyncio and the sanic framework. These applications require Python 3.5 | ||
or later. | ||
|
||
Note that because sanic currently does not support the WebSocket protocol, | ||
the only available transport is long-polling at this time. | ||
|
||
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, create a virtual environment, install the requirements | ||
and then run:: | ||
|
||
$ python simple.py | ||
|
||
or:: | ||
|
||
$ python latency.py | ||
|
||
You can then access the application from your web browser at | ||
``http://localhost:8000``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>EIO Latency</title> | ||
<link rel="stylesheet" href="/static/style.css" /> | ||
</head> | ||
<body> | ||
<h1>EIO Latency <span id="latency"></span></h1> | ||
<h2 id="transport">(connecting)</h2> | ||
<canvas id="chart" height="200"></canvas> | ||
|
||
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script> | ||
<script src="//cdnjs.cloudflare.com/ajax/libs/smoothie/1.27.0/smoothie.js"></script> | ||
<script src="/static/engine.io.js"></script> | ||
<script> | ||
// socket | ||
var socket = eio('http://' + document.domain + ':' + location.port); | ||
var char = $('chart').get(0); | ||
socket.on('open', function() { | ||
if (chart.getContext) { | ||
render(); | ||
window.onresize = render; | ||
} | ||
send(); | ||
}); | ||
socket.on('message', function(data) { | ||
var latency = new Date - last; | ||
$('#latency').text(latency + 'ms'); | ||
if (time) | ||
time.append(+new Date, latency); | ||
setTimeout(send, 100); | ||
}); | ||
socket.on('close', function() { | ||
if (smoothie) | ||
smoothie.stop(); | ||
$('#transport').text('(disconnected)'); | ||
}); | ||
|
||
var last; | ||
function send() { | ||
last = new Date; | ||
socket.send('ping'); | ||
$('#transport').text(socket.transport.name); | ||
} | ||
|
||
// chart | ||
var smoothie; | ||
var time; | ||
function render() { | ||
if (smoothie) | ||
smoothie.stop(); | ||
chart.width = document.body.clientWidth; | ||
smoothie = new SmoothieChart(); | ||
smoothie.streamTo(chart, 1000); | ||
time = new TimeSeries(); | ||
smoothie.addTimeSeries(time, { | ||
strokeStyle: 'rgb(255, 0, 0)', | ||
fillStyle: 'rgba(255, 0, 0, 0.4)', | ||
lineWidth: 2 | ||
}); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from sanic import Sanic | ||
from sanic.response import HTTPResponse | ||
|
||
import engineio | ||
|
||
eio = engineio.AsyncServer(async_mode='sanic') | ||
app = Sanic() | ||
eio.attach(app) | ||
|
||
|
||
@app.route('/') | ||
async def index(request): | ||
with open('latency.html') as f: | ||
return HTTPResponse(body=f.read(), content_type='text/html') | ||
|
||
|
||
@eio.on('message') | ||
async def message(sid, data): | ||
await eio.send(sid, 'pong', binary=False) | ||
|
||
|
||
app.static('/static', './static') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
aiofiles==0.3.0 | ||
httptools==0.0.9 | ||
-e [email protected]:miguelgrinberg/python-engineio@b2da2283451d558298cae888b9ef148186830a7e#egg=python_engineio | ||
sanic==0.3.1 | ||
six==1.10.0 | ||
ujson==1.35 | ||
uvloop==0.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<html> | ||
<head> | ||
<script src="/static/engine.io.js"></script> | ||
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script> | ||
<script> | ||
$(document).ready(function() { | ||
var socket = eio('http://' + document.domain + ':' + location.port); | ||
socket.on('open', function() { | ||
$('#log').append("Connected to server.<br>"); | ||
}); | ||
socket.on('message', function(data) { | ||
$('#log').append("Server says: " + data + "<br>"); | ||
}); | ||
socket.on('close', function() { | ||
$('#log').append("Server closed the connection.<br>"); | ||
}); | ||
window.setInterval(function() { | ||
$('#log').append("Sending message to server...<br>"); | ||
socket.send('hello from client side!'); | ||
}, 5000); | ||
}); | ||
</script> | ||
</head> | ||
<body> | ||
<h1>python-engineio example application</h1> | ||
<p id="log"></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from sanic import Sanic | ||
from sanic.response import HTTPResponse | ||
|
||
import engineio | ||
|
||
eio = engineio.AsyncServer(async_mode='sanic') | ||
app = Sanic() | ||
eio.attach(app) | ||
|
||
|
||
@app.route('/') | ||
async def index(request): | ||
with open('simple.html') as f: | ||
return HTTPResponse(body=f.read(), content_type='text/html') | ||
|
||
|
||
@eio.on('connect') | ||
def connect(sid, environ): | ||
print("connect ", sid) | ||
|
||
|
||
@eio.on('message') | ||
async def message(sid, data): | ||
print('message from', sid, data) | ||
await eio.send(sid, 'Thank you for your message!', binary=False) | ||
|
||
|
||
@eio.on('disconnect') | ||
def disconnect(sid): | ||
print('disconnect ', sid) | ||
|
||
|
||
app.static('/static', './static') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
Oops, something went wrong.