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')
@sio.on('connect')
async def on_connect():
@sio.event
async def connect():
print('connected to server')
await send_ping()
@sio.on('pong_from_server')
async def on_pong(data):
@sio.event
async def pong_from_server(data):
global start_timer
latency = time.time() - start_timer
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.on('connect')
def on_connect():
@sio.event
def connect():
print('connected to server')
send_ping()
@sio.on('pong_from_server')
def on_pong(data):
@sio.event
def pong_from_server(data):
global start_timer
latency = time.time() - start_timer
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" charset="utf-8">
$(document).ready(function(){
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
var socket = io.connect();
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
socket.emit('my_event', {data: 'I\'m connected!'});
});
socket.on('disconnect', function() {
$('#log').append('<br>Disconnected');
});
socket.on('my response', function(msg) {
socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});
@ -24,11 +23,11 @@
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()});
socket.emit('my_event', {data: $('#emit_data').val()});
return false;
});
$('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;
});
$('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false;
});
$('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;
});
$('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()});
socket.emit('close_room', {room: $('#close_room').val()});
return false;
});
$('form#disconnect').submit(function(event) {
socket.emit('disconnect request');
socket.emit('disconnect_request');
return false;
});
});

65
examples/server/aiohttp/app.py

@ -15,8 +15,7 @@ async def background_task():
while True:
await sio.sleep(10)
count += 1
await sio.emit('my response', {'data': 'Server generated event'},
namespace='/test')
await sio.emit('my_response', {'data': 'Server generated event'})
async def index(request):
@ -24,58 +23,56 @@ async def index(request):
return web.Response(text=f.read(), content_type='text/html')
@sio.on('my event', namespace='/test')
async def test_message(sid, message):
await sio.emit('my response', {'data': message['data']}, room=sid,
namespace='/test')
@sio.event
async def my_event(sid, message):
await sio.emit('my_response', {'data': message['data']}, room=sid)
@sio.on('my broadcast event', namespace='/test')
async def test_broadcast_message(sid, message):
await sio.emit('my response', {'data': message['data']}, namespace='/test')
@sio.event
async def my_broadcast_event(sid, message):
await sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test')
@sio.event
async def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test')
await sio.emit('my response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test')
sio.enter_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid)
@sio.on('leave', namespace='/test')
@sio.event
async def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test')
await sio.emit('my response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test')
sio.leave_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid)
@sio.on('close room', namespace='/test')
async def close(sid, message):
await sio.emit('my response',
@sio.event
async def close_room(sid, message):
await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test')
await sio.close_room(message['room'], namespace='/test')
room=message['room'])
await sio.close_room(message['room'])
@sio.on('my room event', namespace='/test')
async def send_room_message(sid, message):
await sio.emit('my response', {'data': message['data']},
room=message['room'], namespace='/test')
@sio.event
async def my_room_event(sid, message):
await sio.emit('my_response', {'data': message['data']},
room=message['room'])
@sio.on('disconnect request', namespace='/test')
@sio.event
async def disconnect_request(sid):
await sio.disconnect(sid, namespace='/test')
await sio.disconnect(sid)
@sio.on('connect', namespace='/test')
async def test_connect(sid, environ):
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid,
namespace='/test')
@sio.event
async def connect(sid, environ):
await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
@sio.on('disconnect', namespace='/test')
def test_disconnect(sid):
@sio.event
def disconnect(sid):
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>
// socket
var socket = io.connect('http://' + document.domain + ':' + location.port);
var socket = io.connect();
var char = $('chart').get(0);
socket.on('connect', function() {
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')
@sio.on('ping_from_client')
async def ping(sid):
@sio.event
async def ping_from_client(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" charset="utf-8">
$(document).ready(function(){
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
var socket = io.connect();
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
socket.emit('my_event', {data: 'I\'m connected!'});
});
socket.on('disconnect', function() {
$('#log').append('<br>Disconnected');
});
socket.on('my response', function(msg) {
socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});
@ -24,11 +23,11 @@
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()});
socket.emit('my_event', {data: $('#emit_data').val()});
return false;
});
$('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;
});
$('form#join').submit(function(event) {
@ -40,7 +39,7 @@
return false;
});
$('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;
});
$('form#close').submit(function(event) {

53
examples/server/asgi/app.py

@ -18,65 +18,62 @@ async def background_task():
while True:
await sio.sleep(10)
count += 1
await sio.emit('my response', {'data': 'Server generated event'},
namespace='/test')
await sio.emit('my_response', {'data': 'Server generated event'})
@sio.on('my event', namespace='/test')
@sio.on('my_event')
async def test_message(sid, message):
await sio.emit('my response', {'data': message['data']}, room=sid,
namespace='/test')
await sio.emit('my_response', {'data': message['data']}, room=sid)
@sio.on('my broadcast event', namespace='/test')
@sio.on('my_broadcast_event')
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):
sio.enter_room(sid, message['room'], namespace='/test')
await sio.emit('my response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test')
sio.enter_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid)
@sio.on('leave', namespace='/test')
@sio.on('leave')
async def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test')
await sio.emit('my response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test')
sio.leave_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid)
@sio.on('close room', namespace='/test')
@sio.on('close room')
async def close(sid, message):
await sio.emit('my response',
await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test')
await sio.close_room(message['room'], namespace='/test')
room=message['room'])
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):
await sio.emit('my response', {'data': message['data']},
room=message['room'], namespace='/test')
await sio.emit('my_response', {'data': message['data']},
room=message['room'])
@sio.on('disconnect request', namespace='/test')
@sio.on('disconnect request')
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):
global background_task_started
if not background_task_started:
sio.start_background_task(background_task)
background_task_started = True
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid,
namespace='/test')
await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
@sio.on('disconnect', namespace='/test')
@sio.on('disconnect')
def test_disconnect(sid):
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>
// socket
var socket = io.connect('http://' + document.domain + ':' + location.port);
var socket = io.connect();
var char = $('chart').get(0);
socket.on('connect', function() {
if (chart.getContext) {

4
examples/server/asgi/latency.py

@ -10,8 +10,8 @@ app = socketio.ASGIApp(sio, static_files={
})
@sio.on('ping_from_client')
async def ping(sid):
@sio.event
async def ping_from_client(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="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
$(document).ready(function() {
var socket = io.connect();
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
socket.emit('my_event', {data: 'I\'m connected!'});
});
socket.on('disconnect', function() {
$('#log').append('<br>Disconnected');
});
socket.on('my response', function(msg) {
socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});
@ -24,11 +23,11 @@
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()});
socket.emit('my_event', {data: $('#emit_data').val()});
return false;
});
$('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;
});
$('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false;
});
$('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;
});
$('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()});
socket.emit('close_room', {room: $('#close_room').val()});
return false;
});
$('form#disconnect').submit(function(event) {
socket.emit('disconnect request');
socket.emit('disconnect_request');
return false;
});
});

65
examples/server/sanic/app.py

@ -16,8 +16,7 @@ async def background_task():
while True:
await sio.sleep(10)
count += 1
await sio.emit('my response', {'data': 'Server generated event'},
namespace='/test')
await sio.emit('my_response', {'data': 'Server generated event'})
@app.listener('before_server_start')
@ -31,58 +30,56 @@ async def index(request):
return html(f.read())
@sio.on('my event', namespace='/test')
async def test_message(sid, message):
await sio.emit('my response', {'data': message['data']}, room=sid,
namespace='/test')
@sio.event
async def my_event(sid, message):
await sio.emit('my_response', {'data': message['data']}, room=sid)
@sio.on('my broadcast event', namespace='/test')
async def test_broadcast_message(sid, message):
await sio.emit('my response', {'data': message['data']}, namespace='/test')
@sio.event
async def my_broadcast_event(sid, message):
await sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test')
@sio.event
async def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test')
await sio.emit('my response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test')
sio.enter_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid)
@sio.on('leave', namespace='/test')
@sio.event
async def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test')
await sio.emit('my response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test')
sio.leave_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid)
@sio.on('close room', namespace='/test')
async def close(sid, message):
await sio.emit('my response',
@sio.event
async def close_room(sid, message):
await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test')
await sio.close_room(message['room'], namespace='/test')
room=message['room'])
await sio.close_room(message['room'])
@sio.on('my room event', namespace='/test')
async def send_room_message(sid, message):
await sio.emit('my response', {'data': message['data']},
room=message['room'], namespace='/test')
@sio.event
async def my_room_event(sid, message):
await sio.emit('my_response', {'data': message['data']},
room=message['room'])
@sio.on('disconnect request', namespace='/test')
@sio.event
async def disconnect_request(sid):
await sio.disconnect(sid, namespace='/test')
await sio.disconnect(sid)
@sio.on('connect', namespace='/test')
async def test_connect(sid, environ):
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid,
namespace='/test')
@sio.event
async def connect(sid, environ):
await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
@sio.on('disconnect', namespace='/test')
def test_disconnect(sid):
@sio.event
def disconnect(sid):
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>
// socket
var socket = io.connect('http://' + document.domain + ':' + location.port);
var socket = io.connect();
var char = $('chart').get(0);
socket.on('connect', function() {
if (chart.getContext) {

4
examples/server/sanic/latency.py

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

66
examples/server/tornado/app.py

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

4
examples/server/tornado/latency.py

@ -17,8 +17,8 @@ class MainHandler(tornado.web.RequestHandler):
self.render("latency.html")
@sio.on('ping_from_client')
async def ping(sid):
@sio.event
async def ping_from_client(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" charset="utf-8">
$(document).ready(function(){
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
var socket = io.connect();
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
socket.emit('my_event', {data: 'I\'m connected!'});
});
socket.on('disconnect', function() {
$('#log').append('<br>Disconnected');
});
socket.on('my response', function(msg) {
socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});
@ -24,11 +23,11 @@
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()});
socket.emit('my_event', {data: $('#emit_data').val()});
return false;
});
$('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;
});
$('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false;
});
$('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;
});
$('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()});
socket.emit('close_room', {room: $('#close_room').val()});
return false;
});
$('form#disconnect').submit(function(event) {
socket.emit('disconnect request');
socket.emit('disconnect_request');
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>
// socket
var socket = io.connect('http://' + document.domain + ':' + location.port);
var socket = io.connect();
var char = $('chart').get(0);
socket.on('connect', function() {
if (chart.getContext) {

64
examples/server/wsgi/app.py

@ -20,8 +20,7 @@ def background_thread():
while True:
sio.sleep(10)
count += 1
sio.emit('my response', {'data': 'Server generated event'},
namespace='/test')
sio.emit('my_response', {'data': 'Server generated event'})
@app.route('/')
@ -32,58 +31,55 @@ def index():
return render_template('index.html')
@sio.on('my event', namespace='/test')
def test_message(sid, message):
sio.emit('my response', {'data': message['data']}, room=sid,
namespace='/test')
@sio.event
def my_event(sid, message):
sio.emit('my_response', {'data': message['data']}, room=sid)
@sio.on('my broadcast event', namespace='/test')
def test_broadcast_message(sid, message):
sio.emit('my response', {'data': message['data']}, namespace='/test')
@sio.event
def my_broadcast_event(sid, message):
sio.emit('my_response', {'data': message['data']})
@sio.on('join', namespace='/test')
@sio.event
def join(sid, message):
sio.enter_room(sid, message['room'], namespace='/test')
sio.emit('my response', {'data': 'Entered room: ' + message['room']},
room=sid, namespace='/test')
sio.enter_room(sid, message['room'])
sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid)
@sio.on('leave', namespace='/test')
@sio.event
def leave(sid, message):
sio.leave_room(sid, message['room'], namespace='/test')
sio.emit('my response', {'data': 'Left room: ' + message['room']},
room=sid, namespace='/test')
sio.leave_room(sid, message['room'])
sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid)
@sio.on('close room', namespace='/test')
def close(sid, message):
sio.emit('my response',
@sio.event
def close_room(sid, message):
sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test')
sio.close_room(message['room'], namespace='/test')
room=message['room'])
sio.close_room(message['room'])
@sio.on('my room event', namespace='/test')
def send_room_message(sid, message):
sio.emit('my response', {'data': message['data']}, room=message['room'],
namespace='/test')
@sio.event
def my_room_event(sid, message):
sio.emit('my_response', {'data': message['data']}, room=message['room'])
@sio.on('disconnect request', namespace='/test')
@sio.event
def disconnect_request(sid):
sio.disconnect(sid, namespace='/test')
sio.disconnect(sid)
@sio.on('connect', namespace='/test')
def test_connect(sid, environ):
sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid,
namespace='/test')
@sio.event
def connect(sid, environ):
sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
@sio.on('disconnect', namespace='/test')
def test_disconnect(sid):
@sio.event
def disconnect(sid):
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" charset="utf-8">
$(document).ready(function(){
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
var socket = io.connect();
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
socket.emit('my_event', {data: 'I\'m connected!'});
});
socket.on('disconnect', function() {
$('#log').append('<br>Disconnected');
});
socket.on('my response', function(msg) {
socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});
@ -24,11 +23,11 @@
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()});
socket.emit('my_event', {data: $('#emit_data').val()});
return false;
});
$('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;
});
$('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false;
});
$('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;
});
$('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()});
socket.emit('close_room', {room: $('#close_room').val()});
return false;
});
$('form#disconnect').submit(function(event) {
socket.emit('disconnect request');
socket.emit('disconnect_request');
return false;
});
});

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

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

4
examples/server/wsgi/latency.py

@ -16,8 +16,8 @@ def index():
return render_template('latency.html')
@sio.on('ping_from_client')
def ping(sid):
@sio.event
def ping_from_client(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" charset="utf-8">
$(document).ready(function(){
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
var socket = io.connect();
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
socket.emit('my_event', {data: 'I\'m connected!'});
});
socket.on('disconnect', function() {
$('#log').append('<br>Disconnected');
});
socket.on('my response', function(msg) {
socket.on('my_response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});
@ -24,11 +23,11 @@
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()});
socket.emit('my_event', {data: $('#emit_data').val()});
return false;
});
$('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;
});
$('form#join').submit(function(event) {
@ -40,15 +39,15 @@
return false;
});
$('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;
});
$('form#close').submit(function(event) {
socket.emit('close room', {room: $('#close_room').val()});
socket.emit('close_room', {room: $('#close_room').val()});
return false;
});
$('form#disconnect').submit(function(event) {
socket.emit('disconnect request');
socket.emit('disconnect_request');
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>
// socket
var socket = io.connect('http://' + document.domain + ':' + location.port);
var socket = io.connect();
var chart = $('#chart').get(0);
socket.on('connect', function() {
if (chart.getContext) {

Loading…
Cancel
Save