Browse Source

Various simplifications to examples

pull/319/head
Miguel Grinberg 6 years ago
parent
commit
0f42c181da
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 8
      examples/client/asyncio/latency_client.py
  2. 8
      examples/client/threads/latency_client.py
  3. 17
      examples/server/aiohttp/app.html
  4. 65
      examples/server/aiohttp/app.py
  5. 2
      examples/server/aiohttp/latency.html
  6. 4
      examples/server/aiohttp/latency.py
  7. 13
      examples/server/asgi/app.html
  8. 53
      examples/server/asgi/app.py
  9. 2
      examples/server/asgi/latency.html
  10. 4
      examples/server/asgi/latency.py
  11. 19
      examples/server/sanic/app.html
  12. 65
      examples/server/sanic/app.py
  13. 2
      examples/server/sanic/latency.html
  14. 4
      examples/server/sanic/latency.py
  15. 66
      examples/server/tornado/app.py
  16. 4
      examples/server/tornado/latency.py
  17. 17
      examples/server/tornado/templates/app.html
  18. 2
      examples/server/tornado/templates/latency.html
  19. 64
      examples/server/wsgi/app.py
  20. 17
      examples/server/wsgi/django_example/socketio_app/static/index.html
  21. 63
      examples/server/wsgi/django_example/socketio_app/views.py
  22. 4
      examples/server/wsgi/latency.py
  23. 17
      examples/server/wsgi/templates/index.html
  24. 2
      examples/server/wsgi/templates/latency.html

8
examples/client/asyncio/latency_client.py

@ -13,14 +13,14 @@ async def send_ping():
await sio.emit('ping_from_client') await sio.emit('ping_from_client')
@sio.on('connect') @sio.event
async def on_connect(): async def connect():
print('connected to server') print('connected to server')
await send_ping() await send_ping()
@sio.on('pong_from_server') @sio.event
async def on_pong(data): async def pong_from_server(data):
global start_timer global start_timer
latency = time.time() - start_timer latency = time.time() - start_timer
print('latency is {0:.2f} ms'.format(latency * 1000)) print('latency is {0:.2f} ms'.format(latency * 1000))

8
examples/client/threads/latency_client.py

@ -11,14 +11,14 @@ def send_ping():
sio.emit('ping_from_client') sio.emit('ping_from_client')
@sio.on('connect') @sio.event
def on_connect(): def connect():
print('connected to server') print('connected to server')
send_ping() send_ping()
@sio.on('pong_from_server') @sio.event
def on_pong(data): def pong_from_server(data):
global start_timer global start_timer
latency = time.time() - start_timer latency = time.time() - start_timer
print('latency is {0:.2f} ms'.format(latency * 1000)) print('latency is {0:.2f} ms'.format(latency * 1000))

17
examples/server/aiohttp/app.html

@ -6,16 +6,15 @@
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
$(document).ready(function(){ $(document).ready(function(){
namespace = '/test'; var socket = io.connect();
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() { socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'}); socket.emit('my_event', {data: 'I\'m connected!'});
}); });
socket.on('disconnect', function() { socket.on('disconnect', function() {
$('#log').append('<br>Disconnected'); $('#log').append('<br>Disconnected');
}); });
socket.on('my response', function(msg) { socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data); $('#log').append('<br>Received: ' + msg.data);
}); });
@ -24,11 +23,11 @@
// handlers for the different forms in the page // handlers for the different forms in the page
// these send data to the server in a variety of ways // these send data to the server in a variety of ways
$('form#emit').submit(function(event) { $('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()}); socket.emit('my_event', {data: $('#emit_data').val()});
return false; return false;
}); });
$('form#broadcast').submit(function(event) { $('form#broadcast').submit(function(event) {
socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); socket.emit('my_broadcast_event', {data: $('#broadcast_data').val()});
return false; return false;
}); });
$('form#join').submit(function(event) { $('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false; return false;
}); });
$('form#send_room').submit(function(event) { $('form#send_room').submit(function(event) {
socket.emit('my room event', {room: $('#room_name').val(), data: $('#room_data').val()}); socket.emit('my_room_event', {room: $('#room_name').val(), data: $('#room_data').val()});
return false; return false;
}); });
$('form#close').submit(function(event) { $('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()}); socket.emit('close_room', {room: $('#close_room').val()});
return false; return false;
}); });
$('form#disconnect').submit(function(event) { $('form#disconnect').submit(function(event) {
socket.emit('disconnect request'); socket.emit('disconnect_request');
return false; return false;
}); });
}); });

65
examples/server/aiohttp/app.py

@ -15,8 +15,7 @@ async def background_task():
while True: while True:
await sio.sleep(10) await sio.sleep(10)
count += 1 count += 1
await sio.emit('my response', {'data': 'Server generated event'}, await sio.emit('my_response', {'data': 'Server generated event'})
namespace='/test')
async def index(request): async def index(request):
@ -24,58 +23,56 @@ async def index(request):
return web.Response(text=f.read(), content_type='text/html') return web.Response(text=f.read(), content_type='text/html')
@sio.on('my event', namespace='/test') @sio.event
async def test_message(sid, message): async def my_event(sid, message):
await sio.emit('my response', {'data': message['data']}, room=sid, await sio.emit('my_response', {'data': message['data']}, room=sid)
namespace='/test')
@sio.on('my broadcast event', namespace='/test') @sio.event
async def test_broadcast_message(sid, message): async def my_broadcast_event(sid, message):
await sio.emit('my response', {'data': message['data']}, namespace='/test') await sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test') @sio.event
async def join(sid, message): async def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test') sio.enter_room(sid, message['room'])
await sio.emit('my response', {'data': 'Entered room: ' + message['room']}, await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('leave', namespace='/test') @sio.event
async def leave(sid, message): async def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test') sio.leave_room(sid, message['room'])
await sio.emit('my response', {'data': 'Left room: ' + message['room']}, await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('close room', namespace='/test') @sio.event
async def close(sid, message): async def close_room(sid, message):
await sio.emit('my response', await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'}, {'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test') room=message['room'])
await sio.close_room(message['room'], namespace='/test') await sio.close_room(message['room'])
@sio.on('my room event', namespace='/test') @sio.event
async def send_room_message(sid, message): async def my_room_event(sid, message):
await sio.emit('my response', {'data': message['data']}, await sio.emit('my_response', {'data': message['data']},
room=message['room'], namespace='/test') room=message['room'])
@sio.on('disconnect request', namespace='/test') @sio.event
async def disconnect_request(sid): async def disconnect_request(sid):
await sio.disconnect(sid, namespace='/test') await sio.disconnect(sid)
@sio.on('connect', namespace='/test') @sio.event
async def test_connect(sid, environ): async def connect(sid, environ):
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
namespace='/test')
@sio.on('disconnect', namespace='/test') @sio.event
def test_disconnect(sid): def disconnect(sid):
print('Client disconnected') print('Client disconnected')

2
examples/server/aiohttp/latency.html

@ -14,7 +14,7 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script> <script>
// socket // socket
var socket = io.connect('http://' + document.domain + ':' + location.port); var socket = io.connect();
var char = $('chart').get(0); var char = $('chart').get(0);
socket.on('connect', function() { socket.on('connect', function() {
if (chart.getContext) { if (chart.getContext) {

4
examples/server/aiohttp/latency.py

@ -12,8 +12,8 @@ async def index(request):
return web.Response(text=f.read(), content_type='text/html') return web.Response(text=f.read(), content_type='text/html')
@sio.on('ping_from_client') @sio.event
async def ping(sid): async def ping_from_client(sid):
await sio.emit('pong_from_server', room=sid) await sio.emit('pong_from_server', room=sid)

13
examples/server/asgi/app.html

@ -6,16 +6,15 @@
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
$(document).ready(function(){ $(document).ready(function(){
namespace = '/test'; var socket = io.connect();
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() { socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'}); socket.emit('my_event', {data: 'I\'m connected!'});
}); });
socket.on('disconnect', function() { socket.on('disconnect', function() {
$('#log').append('<br>Disconnected'); $('#log').append('<br>Disconnected');
}); });
socket.on('my response', function(msg) { socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data); $('#log').append('<br>Received: ' + msg.data);
}); });
@ -24,11 +23,11 @@
// handlers for the different forms in the page // handlers for the different forms in the page
// these send data to the server in a variety of ways // these send data to the server in a variety of ways
$('form#emit').submit(function(event) { $('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()}); socket.emit('my_event', {data: $('#emit_data').val()});
return false; return false;
}); });
$('form#broadcast').submit(function(event) { $('form#broadcast').submit(function(event) {
socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); socket.emit('my_broadcast_event', {data: $('#broadcast_data').val()});
return false; return false;
}); });
$('form#join').submit(function(event) { $('form#join').submit(function(event) {
@ -40,7 +39,7 @@
return false; return false;
}); });
$('form#send_room').submit(function(event) { $('form#send_room').submit(function(event) {
socket.emit('my room event', {room: $('#room_name').val(), data: $('#room_data').val()}); socket.emit('my_room_event', {room: $('#room_name').val(), data: $('#room_data').val()});
return false; return false;
}); });
$('form#close').submit(function(event) { $('form#close').submit(function(event) {

53
examples/server/asgi/app.py

@ -18,65 +18,62 @@ async def background_task():
while True: while True:
await sio.sleep(10) await sio.sleep(10)
count += 1 count += 1
await sio.emit('my response', {'data': 'Server generated event'}, await sio.emit('my_response', {'data': 'Server generated event'})
namespace='/test')
@sio.on('my event', namespace='/test') @sio.on('my_event')
async def test_message(sid, message): async def test_message(sid, message):
await sio.emit('my response', {'data': message['data']}, room=sid, await sio.emit('my_response', {'data': message['data']}, room=sid)
namespace='/test')
@sio.on('my broadcast event', namespace='/test') @sio.on('my_broadcast_event')
async def test_broadcast_message(sid, message): async def test_broadcast_message(sid, message):
await sio.emit('my response', {'data': message['data']}, namespace='/test') await sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test') @sio.on('join')
async def join(sid, message): async def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test') sio.enter_room(sid, message['room'])
await sio.emit('my response', {'data': 'Entered room: ' + message['room']}, await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('leave', namespace='/test') @sio.on('leave')
async def leave(sid, message): async def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test') sio.leave_room(sid, message['room'])
await sio.emit('my response', {'data': 'Left room: ' + message['room']}, await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('close room', namespace='/test') @sio.on('close room')
async def close(sid, message): async def close(sid, message):
await sio.emit('my response', await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'}, {'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test') room=message['room'])
await sio.close_room(message['room'], namespace='/test') await sio.close_room(message['room'])
@sio.on('my room event', namespace='/test') @sio.on('my_room_event')
async def send_room_message(sid, message): async def send_room_message(sid, message):
await sio.emit('my response', {'data': message['data']}, await sio.emit('my_response', {'data': message['data']},
room=message['room'], namespace='/test') room=message['room'])
@sio.on('disconnect request', namespace='/test') @sio.on('disconnect request')
async def disconnect_request(sid): async def disconnect_request(sid):
await sio.disconnect(sid, namespace='/test') await sio.disconnect(sid)
@sio.on('connect', namespace='/test') @sio.on('connect')
async def test_connect(sid, environ): async def test_connect(sid, environ):
global background_task_started global background_task_started
if not background_task_started: if not background_task_started:
sio.start_background_task(background_task) sio.start_background_task(background_task)
background_task_started = True background_task_started = True
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
namespace='/test')
@sio.on('disconnect', namespace='/test') @sio.on('disconnect')
def test_disconnect(sid): def test_disconnect(sid):
print('Client disconnected') print('Client disconnected')

2
examples/server/asgi/latency.html

@ -14,7 +14,7 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script> <script>
// socket // socket
var socket = io.connect('http://' + document.domain + ':' + location.port); var socket = io.connect();
var char = $('chart').get(0); var char = $('chart').get(0);
socket.on('connect', function() { socket.on('connect', function() {
if (chart.getContext) { if (chart.getContext) {

4
examples/server/asgi/latency.py

@ -10,8 +10,8 @@ app = socketio.ASGIApp(sio, static_files={
}) })
@sio.on('ping_from_client') @sio.event
async def ping(sid): async def ping_from_client(sid):
await sio.emit('pong_from_server', room=sid) await sio.emit('pong_from_server', room=sid)

19
examples/server/sanic/app.html

@ -5,17 +5,16 @@
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script> <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
$(document).ready(function(){ $(document).ready(function() {
namespace = '/test'; var socket = io.connect();
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() { socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'}); socket.emit('my_event', {data: 'I\'m connected!'});
}); });
socket.on('disconnect', function() { socket.on('disconnect', function() {
$('#log').append('<br>Disconnected'); $('#log').append('<br>Disconnected');
}); });
socket.on('my response', function(msg) { socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data); $('#log').append('<br>Received: ' + msg.data);
}); });
@ -24,11 +23,11 @@
// handlers for the different forms in the page // handlers for the different forms in the page
// these send data to the server in a variety of ways // these send data to the server in a variety of ways
$('form#emit').submit(function(event) { $('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()}); socket.emit('my_event', {data: $('#emit_data').val()});
return false; return false;
}); });
$('form#broadcast').submit(function(event) { $('form#broadcast').submit(function(event) {
socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); socket.emit('my_broadcast_event', {data: $('#broadcast_data').val()});
return false; return false;
}); });
$('form#join').submit(function(event) { $('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false; return false;
}); });
$('form#send_room').submit(function(event) { $('form#send_room').submit(function(event) {
socket.emit('my room event', {room: $('#room_name').val(), data: $('#room_data').val()}); socket.emit('my_room_event', {room: $('#room_name').val(), data: $('#room_data').val()});
return false; return false;
}); });
$('form#close').submit(function(event) { $('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()}); socket.emit('close_room', {room: $('#close_room').val()});
return false; return false;
}); });
$('form#disconnect').submit(function(event) { $('form#disconnect').submit(function(event) {
socket.emit('disconnect request'); socket.emit('disconnect_request');
return false; return false;
}); });
}); });

65
examples/server/sanic/app.py

@ -16,8 +16,7 @@ async def background_task():
while True: while True:
await sio.sleep(10) await sio.sleep(10)
count += 1 count += 1
await sio.emit('my response', {'data': 'Server generated event'}, await sio.emit('my_response', {'data': 'Server generated event'})
namespace='/test')
@app.listener('before_server_start') @app.listener('before_server_start')
@ -31,58 +30,56 @@ async def index(request):
return html(f.read()) return html(f.read())
@sio.on('my event', namespace='/test') @sio.event
async def test_message(sid, message): async def my_event(sid, message):
await sio.emit('my response', {'data': message['data']}, room=sid, await sio.emit('my_response', {'data': message['data']}, room=sid)
namespace='/test')
@sio.on('my broadcast event', namespace='/test') @sio.event
async def test_broadcast_message(sid, message): async def my_broadcast_event(sid, message):
await sio.emit('my response', {'data': message['data']}, namespace='/test') await sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test') @sio.event
async def join(sid, message): async def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test') sio.enter_room(sid, message['room'])
await sio.emit('my response', {'data': 'Entered room: ' + message['room']}, await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('leave', namespace='/test') @sio.event
async def leave(sid, message): async def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test') sio.leave_room(sid, message['room'])
await sio.emit('my response', {'data': 'Left room: ' + message['room']}, await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('close room', namespace='/test') @sio.event
async def close(sid, message): async def close_room(sid, message):
await sio.emit('my response', await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'}, {'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test') room=message['room'])
await sio.close_room(message['room'], namespace='/test') await sio.close_room(message['room'])
@sio.on('my room event', namespace='/test') @sio.event
async def send_room_message(sid, message): async def my_room_event(sid, message):
await sio.emit('my response', {'data': message['data']}, await sio.emit('my_response', {'data': message['data']},
room=message['room'], namespace='/test') room=message['room'])
@sio.on('disconnect request', namespace='/test') @sio.event
async def disconnect_request(sid): async def disconnect_request(sid):
await sio.disconnect(sid, namespace='/test') await sio.disconnect(sid)
@sio.on('connect', namespace='/test') @sio.event
async def test_connect(sid, environ): async def connect(sid, environ):
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
namespace='/test')
@sio.on('disconnect', namespace='/test') @sio.event
def test_disconnect(sid): def disconnect(sid):
print('Client disconnected') print('Client disconnected')

2
examples/server/sanic/latency.html

@ -14,7 +14,7 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script> <script>
// socket // socket
var socket = io.connect('http://' + document.domain + ':' + location.port); var socket = io.connect();
var char = $('chart').get(0); var char = $('chart').get(0);
socket.on('connect', function() { socket.on('connect', function() {
if (chart.getContext) { if (chart.getContext) {

4
examples/server/sanic/latency.py

@ -14,8 +14,8 @@ def index(request):
return html(f.read()) return html(f.read())
@sio.on('ping_from_client') @sio.event
async def ping(sid): async def ping_from_client(sid):
await sio.emit('pong_from_server', room=sid) await sio.emit('pong_from_server', room=sid)
app.static('/static', './static') app.static('/static', './static')

66
examples/server/tornado/app.py

@ -18,8 +18,7 @@ async def background_task():
while True: while True:
await sio.sleep(10) await sio.sleep(10)
count += 1 count += 1
await sio.emit('my response', {'data': 'Server generated event'}, await sio.emit('my_response', {'data': 'Server generated event'})
namespace='/test')
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
@ -27,58 +26,55 @@ class MainHandler(tornado.web.RequestHandler):
self.render("app.html") self.render("app.html")
@sio.on('my event', namespace='/test') @sio.event
async def test_message(sid, message): async def my_event(sid, message):
await sio.emit('my response', {'data': message['data']}, room=sid, await sio.emit('my_response', {'data': message['data']}, room=sid)
namespace='/test')
@sio.on('my broadcast event', namespace='/test') @sio.event
async def test_broadcast_message(sid, message): async def my_broadcast_event(sid, message):
await sio.emit('my response', {'data': message['data']}, namespace='/test') await sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test') @sio.event
async def join(sid, message): async def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test') sio.enter_room(sid, message['room'])
await sio.emit('my response', {'data': 'Entered room: ' + message['room']}, await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.event
@sio.on('leave', namespace='/test')
async def leave(sid, message): async def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test') sio.leave_room(sid, message['room'])
await sio.emit('my response', {'data': 'Left room: ' + message['room']}, await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('close room', namespace='/test') @sio.event
async def close(sid, message): async def close_room(sid, message):
await sio.emit('my response', await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'}, {'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test') room=message['room'])
await sio.close_room(message['room'], namespace='/test') await sio.close_room(message['room'])
@sio.on('my room event', namespace='/test') @sio.event
async def send_room_message(sid, message): async def my_room_event(sid, message):
await sio.emit('my response', {'data': message['data']}, await sio.emit('my_response', {'data': message['data']},
room=message['room'], namespace='/test') room=message['room'])
@sio.on('disconnect request', namespace='/test') @sio.event
async def disconnect_request(sid): async def disconnect_request(sid):
await sio.disconnect(sid, namespace='/test') await sio.disconnect(sid)
@sio.on('connect', namespace='/test') @sio.event
async def test_connect(sid, environ): async def connect(sid, environ):
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
namespace='/test')
@sio.on('disconnect', namespace='/test') @sio.event
def test_disconnect(sid): def disconnect(sid):
print('Client disconnected') print('Client disconnected')

4
examples/server/tornado/latency.py

@ -17,8 +17,8 @@ class MainHandler(tornado.web.RequestHandler):
self.render("latency.html") self.render("latency.html")
@sio.on('ping_from_client') @sio.event
async def ping(sid): async def ping_from_client(sid):
await sio.emit('pong_from_server', room=sid) await sio.emit('pong_from_server', room=sid)

17
examples/server/tornado/templates/app.html

@ -6,16 +6,15 @@
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
$(document).ready(function(){ $(document).ready(function(){
namespace = '/test'; var socket = io.connect();
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() { socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'}); socket.emit('my_event', {data: 'I\'m connected!'});
}); });
socket.on('disconnect', function() { socket.on('disconnect', function() {
$('#log').append('<br>Disconnected'); $('#log').append('<br>Disconnected');
}); });
socket.on('my response', function(msg) { socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data); $('#log').append('<br>Received: ' + msg.data);
}); });
@ -24,11 +23,11 @@
// handlers for the different forms in the page // handlers for the different forms in the page
// these send data to the server in a variety of ways // these send data to the server in a variety of ways
$('form#emit').submit(function(event) { $('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()}); socket.emit('my_event', {data: $('#emit_data').val()});
return false; return false;
}); });
$('form#broadcast').submit(function(event) { $('form#broadcast').submit(function(event) {
socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); socket.emit('my_broadcast_event', {data: $('#broadcast_data').val()});
return false; return false;
}); });
$('form#join').submit(function(event) { $('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false; return false;
}); });
$('form#send_room').submit(function(event) { $('form#send_room').submit(function(event) {
socket.emit('my room event', {room: $('#room_name').val(), data: $('#room_data').val()}); socket.emit('my_room_event', {room: $('#room_name').val(), data: $('#room_data').val()});
return false; return false;
}); });
$('form#close').submit(function(event) { $('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()}); socket.emit('close_room', {room: $('#close_room').val()});
return false; return false;
}); });
$('form#disconnect').submit(function(event) { $('form#disconnect').submit(function(event) {
socket.emit('disconnect request'); socket.emit('disconnect_request');
return false; return false;
}); });
}); });

2
examples/server/tornado/templates/latency.html

@ -14,7 +14,7 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script> <script>
// socket // socket
var socket = io.connect('http://' + document.domain + ':' + location.port); var socket = io.connect();
var char = $('chart').get(0); var char = $('chart').get(0);
socket.on('connect', function() { socket.on('connect', function() {
if (chart.getContext) { if (chart.getContext) {

64
examples/server/wsgi/app.py

@ -20,8 +20,7 @@ def background_thread():
while True: while True:
sio.sleep(10) sio.sleep(10)
count += 1 count += 1
sio.emit('my response', {'data': 'Server generated event'}, sio.emit('my_response', {'data': 'Server generated event'})
namespace='/test')
@app.route('/') @app.route('/')
@ -32,58 +31,55 @@ def index():
return render_template('index.html') return render_template('index.html')
@sio.on('my event', namespace='/test') @sio.event
def test_message(sid, message): def my_event(sid, message):
sio.emit('my response', {'data': message['data']}, room=sid, sio.emit('my_response', {'data': message['data']}, room=sid)
namespace='/test')
@sio.on('my broadcast event', namespace='/test') @sio.event
def test_broadcast_message(sid, message): def my_broadcast_event(sid, message):
sio.emit('my response', {'data': message['data']}, namespace='/test') sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test') @sio.event
def join(sid, message): def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test') sio.enter_room(sid, message['room'])
sio.emit('my response', {'data': 'Entered room: ' + message['room']}, sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('leave', namespace='/test') @sio.event
def leave(sid, message): def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test') sio.leave_room(sid, message['room'])
sio.emit('my response', {'data': 'Left room: ' + message['room']}, sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('close room', namespace='/test') @sio.event
def close(sid, message): def close_room(sid, message):
sio.emit('my response', sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'}, {'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test') room=message['room'])
sio.close_room(message['room'], namespace='/test') sio.close_room(message['room'])
@sio.on('my room event', namespace='/test') @sio.event
def send_room_message(sid, message): def my_room_event(sid, message):
sio.emit('my response', {'data': message['data']}, room=message['room'], sio.emit('my_response', {'data': message['data']}, room=message['room'])
namespace='/test')
@sio.on('disconnect request', namespace='/test') @sio.event
def disconnect_request(sid): def disconnect_request(sid):
sio.disconnect(sid, namespace='/test') sio.disconnect(sid)
@sio.on('connect', namespace='/test') @sio.event
def test_connect(sid, environ): def connect(sid, environ):
sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
namespace='/test')
@sio.on('disconnect', namespace='/test') @sio.event
def test_disconnect(sid): def disconnect(sid):
print('Client disconnected') print('Client disconnected')

17
examples/server/wsgi/django_example/socketio_app/static/index.html

@ -6,16 +6,15 @@
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
$(document).ready(function(){ $(document).ready(function(){
namespace = '/test'; var socket = io.connect();
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() { socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'}); socket.emit('my_event', {data: 'I\'m connected!'});
}); });
socket.on('disconnect', function() { socket.on('disconnect', function() {
$('#log').append('<br>Disconnected'); $('#log').append('<br>Disconnected');
}); });
socket.on('my response', function(msg) { socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data); $('#log').append('<br>Received: ' + msg.data);
}); });
@ -24,11 +23,11 @@
// handlers for the different forms in the page // handlers for the different forms in the page
// these send data to the server in a variety of ways // these send data to the server in a variety of ways
$('form#emit').submit(function(event) { $('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()}); socket.emit('my_event', {data: $('#emit_data').val()});
return false; return false;
}); });
$('form#broadcast').submit(function(event) { $('form#broadcast').submit(function(event) {
socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); socket.emit('my_broadcast_event', {data: $('#broadcast_data').val()});
return false; return false;
}); });
$('form#join').submit(function(event) { $('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false; return false;
}); });
$('form#send_room').submit(function(event) { $('form#send_room').submit(function(event) {
socket.emit('my room event', {room: $('#room_name').val(), data: $('#room_data').val()}); socket.emit('my_room_event', {room: $('#room_name').val(), data: $('#room_data').val()});
return false; return false;
}); });
$('form#close').submit(function(event) { $('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()}); socket.emit('close_room', {room: $('#close_room').val()});
return false; return false;
}); });
$('form#disconnect').submit(function(event) { $('form#disconnect').submit(function(event) {
socket.emit('disconnect request'); socket.emit('disconnect_request');
return false; return false;
}); });
}); });

63
examples/server/wsgi/django_example/socketio_app/views.py

@ -26,61 +26,58 @@ def background_thread():
while True: while True:
sio.sleep(10) sio.sleep(10)
count += 1 count += 1
sio.emit('my response', {'data': 'Server generated event'}, sio.emit('my_response', {'data': 'Server generated event'},
namespace='/test') namespace='/test')
@sio.on('my event', namespace='/test') @sio.event
def test_message(sid, message): def my_event(sid, message):
sio.emit('my response', {'data': message['data']}, room=sid, sio.emit('my_response', {'data': message['data']}, room=sid)
namespace='/test')
@sio.on('my broadcast event', namespace='/test') @sio.event
def test_broadcast_message(sid, message): def my_broadcast_event(sid, message):
sio.emit('my response', {'data': message['data']}, namespace='/test') sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test') @sio.event
def join(sid, message): def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test') sio.enter_room(sid, message['room'])
sio.emit('my response', {'data': 'Entered room: ' + message['room']}, sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('leave', namespace='/test') @sio.event
def leave(sid, message): def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test') sio.leave_room(sid, message['room'])
sio.emit('my response', {'data': 'Left room: ' + message['room']}, sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test') room=sid)
@sio.on('close room', namespace='/test') @sio.event
def close(sid, message): def close_room(sid, message):
sio.emit('my response', sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'}, {'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test') room=message['room'])
sio.close_room(message['room'], namespace='/test') sio.close_room(message['room'])
@sio.on('my room event', namespace='/test') @sio.event
def send_room_message(sid, message): def my_room_event(sid, message):
sio.emit('my response', {'data': message['data']}, room=message['room'], sio.emit('my_response', {'data': message['data']}, room=message['room'])
namespace='/test')
@sio.on('disconnect request', namespace='/test') @sio.event
def disconnect_request(sid): def disconnect_request(sid):
sio.disconnect(sid, namespace='/test') sio.disconnect(sid)
@sio.on('connect', namespace='/test') @sio.event
def test_connect(sid, environ): def connect(sid, environ):
sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
namespace='/test')
@sio.on('disconnect', namespace='/test') @sio.event
def test_disconnect(sid): def disconnect(sid):
print('Client disconnected') print('Client disconnected')

4
examples/server/wsgi/latency.py

@ -16,8 +16,8 @@ def index():
return render_template('latency.html') return render_template('latency.html')
@sio.on('ping_from_client') @sio.event
def ping(sid): def ping_from_client(sid):
sio.emit('pong_from_server', room=sid) sio.emit('pong_from_server', room=sid)

17
examples/server/wsgi/templates/index.html

@ -6,16 +6,15 @@
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
$(document).ready(function(){ $(document).ready(function(){
namespace = '/test'; var socket = io.connect();
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() { socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'}); socket.emit('my_event', {data: 'I\'m connected!'});
}); });
socket.on('disconnect', function() { socket.on('disconnect', function() {
$('#log').append('<br>Disconnected'); $('#log').append('<br>Disconnected');
}); });
socket.on('my response', function(msg) { socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data); $('#log').append('<br>Received: ' + msg.data);
}); });
@ -24,11 +23,11 @@
// handlers for the different forms in the page // handlers for the different forms in the page
// these send data to the server in a variety of ways // these send data to the server in a variety of ways
$('form#emit').submit(function(event) { $('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()}); socket.emit('my_event', {data: $('#emit_data').val()});
return false; return false;
}); });
$('form#broadcast').submit(function(event) { $('form#broadcast').submit(function(event) {
socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); socket.emit('my_broadcast_event', {data: $('#broadcast_data').val()});
return false; return false;
}); });
$('form#join').submit(function(event) { $('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false; return false;
}); });
$('form#send_room').submit(function(event) { $('form#send_room').submit(function(event) {
socket.emit('my room event', {room: $('#room_name').val(), data: $('#room_data').val()}); socket.emit('my_room_event', {room: $('#room_name').val(), data: $('#room_data').val()});
return false; return false;
}); });
$('form#close').submit(function(event) { $('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()}); socket.emit('close_room', {room: $('#close_room').val()});
return false; return false;
}); });
$('form#disconnect').submit(function(event) { $('form#disconnect').submit(function(event) {
socket.emit('disconnect request'); socket.emit('disconnect_request');
return false; return false;
}); });
}); });

2
examples/server/wsgi/templates/latency.html

@ -14,7 +14,7 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script> <script>
// socket // socket
var socket = io.connect('http://' + document.domain + ':' + location.port); var socket = io.connect();
var chart = $('#chart').get(0); var chart = $('#chart').get(0);
socket.on('connect', function() { socket.on('connect', function() {
if (chart.getContext) { if (chart.getContext) {

Loading…
Cancel
Save