Flask-sse

服务器主动推送消息

server.py

import time

from flask import Flask, render_template, jsonify
from flask_sse import sse
from flask_cors import CORS

app = Flask(__name__)
app.config["REDIS_URL"] = "redis://localhost"
app.register_blueprint(sse, url_prefix="/stream")
CORS(app, resources={r"/stream/*": {"origins": "*"}})


@app.route('/')
def index():
    return render_template("index.html")


@app.route('/test')
def test():
    i=0
    while i < 22:
        sse.publish({"message": {"name": "dzg", "age": i}}, type="sseTest")
        time.sleep(0.31)
        i+=1
    return jsonify({"code":200})

@app.route('/test1')
def test1():
    i=0
    while i < 22:
        sse.publish({"message": {"name": "dzg", "age": i*i}}, type="sseTest1")
        time.sleep(0.31)
        i+=1
    return jsonify({"code":200})
if __name__ == '__main__':
    app.run(host="0.0.0.0")

index.html




   flask-sse test


   

progress:

progress:

引自
官网
其它

你可能感兴趣的:(Flask-sse)