我在前端使用事件流。后端的 yield 函数。将客户端存储在 redis 队列中。 我在 redis 队列中正确存储用户,但我不知道向特定登录用户发送推送通知。
我的问题是如何将通知推送给特定的登录用户。
前端代码:
var source = new EventSource('/stream');
source.onmessage = function (event) {
console.log(event)
};
后端代码:
from redis import Redis
redis = Redis()
import time
from datetime import datetime
p = redis.pipeline()
app.config['ONLINE_LAST_MINUTES'] = 5
def check_updates():
yield 'data: %s \n\n' % data
@app.route('/stream')
#@nocache
def stream():
return Response(check_updates(),mimetype='text/event-stream')
最佳答案
这里有一个片段展示了如何使用redis
操作用户:
http://flask.pocoo.org/snippets/71/
如果你使用flask-login
,很容易获取current_user
并通过检查数据库中所有用户的字段返回他/她的指定消息。
关于python - 如何将通知推送给特定的登录用户。我的代码如下所示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28303101/