用于瓶装的事件
Flask-SSE的Python项目详细描述
支持html5server-sent events的烧瓶扩展,由Redis提供支持。
发送事件的示例:
fromflaskimportFlaskfromflask_sseimportsseapp=Flask(__name__)app.config["REDIS_URL"]="redis://localhost"app.register_blueprint(sse,url_prefix='/stream')@app.route('/send')defsend_message():sse.publish({"message":"Hello!"},type='greeting')return"Message sent!"
要接收网页上的事件,请使用javascript连接到事件流, 像这样:
varsource=newEventSource("{{ url_for('sse.stream') }}");source.addEventListener('greeting',function(event){vardata=JSON.parse(event.data);// do what you want with this data },false);
The full documentation for this project is hosted on ReadTheDocs.