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.

110 lines
3.1 KiB

# set this to 'threading', 'eventlet', or 'gevent'
async_mode = 'threading'
if async_mode == 'eventlet':
import eventlet
eventlet.monkey_patch()
elif async_mode == 'gevent':
from gevent import monkey
monkey.patch_all()
import time
from threading import Thread
from flask import Flask, render_template
import socketio
sio = socketio.Server(logger=True, async_mode=async_mode)
app = Flask(__name__)
app.wsgi_app = socketio.Middleware(sio, app.wsgi_app)
app.config['SECRET_KEY'] = 'secret!'
thread = None
def background_thread():
"""Example of how to send server generated events to clients."""
count = 0
while True:
time.sleep(10)
count += 1
sio.emit('my response', {'data': 'Server generated event'},
namespace='/test')
@app.route('/')
def index():
global thread
if thread is None:
thread = Thread(target=background_thread)
thread.start()
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.on('my broadcast event', namespace='/test')
def test_broadcast_message(sid, message):
sio.emit('my response', {'data': message['data']}, namespace='/test')
@sio.on('join', namespace='/test')
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.on('leave', namespace='/test')
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.on('close room', namespace='/test')
def close(sid, message):
sio.emit('my response',
{'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'], namespace='/test')
sio.close_room(message['room'], namespace='/test')
@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.on('disconnect request', namespace='/test')
def disconnect_request(sid):
sio.disconnect(sid, namespace='/test')
@sio.on('connect', namespace='/test')
def test_connect(sid, environ):
sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid,
namespace='/test')
@sio.on('disconnect', namespace='/test')
def test_disconnect(sid):
print('Client disconnected')
if __name__ == '__main__':
if async_mode == 'threading':
# deploy with Werkzeug
app.run(threaded=True)
elif async_mode == 'eventlet':
# deploy with eventlet
import eventlet
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
elif async_mode == 'gevent':
# deploy with gevent
from gevent import pywsgi
pywsgi.WSGIServer(('', 5000), app).serve_forever()
else:
print('Unknown async_mode: ' + async_mode)