pythonasyncioeventletgeventlong-pollinglow-latencysocket-iosocketiosocketio-serverweb-serverwebsocket
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.5 KiB
89 lines
2.5 KiB
import asyncio
|
|
|
|
from sanic import Sanic
|
|
from sanic.response import html
|
|
|
|
import socketio
|
|
|
|
sio = socketio.AsyncServer(async_mode='sanic')
|
|
app = Sanic()
|
|
sio.attach(app)
|
|
|
|
|
|
async def background_task():
|
|
"""Example of how to send server generated events to clients."""
|
|
count = 0
|
|
while True:
|
|
await sio.sleep(10)
|
|
count += 1
|
|
await sio.emit('my response', {'data': 'Server generated event'},
|
|
namespace='/test')
|
|
|
|
|
|
@app.route('/')
|
|
async def index(request):
|
|
with open('app.html') as f:
|
|
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.on('my broadcast event', namespace='/test')
|
|
async def test_broadcast_message(sid, message):
|
|
await sio.emit('my response', {'data': message['data']}, namespace='/test')
|
|
|
|
|
|
@sio.on('join', namespace='/test')
|
|
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.on('leave', namespace='/test')
|
|
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.on('close room', namespace='/test')
|
|
async def close(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')
|
|
|
|
|
|
@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.on('disconnect request', namespace='/test')
|
|
async def disconnect_request(sid):
|
|
await sio.disconnect(sid, namespace='/test')
|
|
|
|
|
|
@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.on('disconnect', namespace='/test')
|
|
def test_disconnect(sid):
|
|
print('Client disconnected')
|
|
|
|
|
|
app.static('/static', './static')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sio.start_background_task(background_task)
|
|
app.run()
|
|
|