Flask服务器使用uwsgi发送事件(SSE)

3

以下内容翻译自: https://pypi.python.org/pypi/Flask-SSE

app.py:


示例代码:
from flask import Flask
from flask_sse import sse

app = Flask(__name__)
app.config["REDIS_URL"] = "redis://localhost"
app.register_blueprint(sse, url_prefix='/stream')

@app.route('/send')
def send_message():
    sse.publish({"message": "Hello!"}, type='greeting')
    return "Message sent!"

使用:

var source = new EventSource("{{ url_for('sse.stream') }}");
source.addEventListener('greeting', function(event) {
    var data = JSON.parse(event.data);
    console.log(data);
}, false);
source.addEventListener('error', function(event) {
    console.log("Failed to connect to event stream.");
}, false);

如果我使用 gunicorn

gunicorn app:app --worker-class gevent --bind 0.0.0.0:8000

在我的模板中可以成功接收消息,但如果我使用 uwsgi

uwsgi --gevent 100 --socket 0.0.0.0:8000 --protocol=http -w wsgi

使用 app.ini 文件:
[uwsgi]
module = wsgi

master = true
processes = 5

socket = app.sock
chmod-socket = 660
vacuum = true

die-on-term = true

模板JavaScript没有收到任何消息,我也没有收到任何错误。

我在Ubuntu 16.10上使用redis服务器。

1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接