1
我有一个使用Server-Sent Events和redis pubsub系统的django项目。 的代码如下:什么是Python服务器发送的事件返回格式?
我用JavaScript代码
var eventSource = new EventSource("/tweets/stream");
eventSource.addEventListener('message', function(e) {
console.log(message)
}, false);
然后在我的Python代码打开的情况下,我通过使用Redis的订阅处理它。当我得到新的数据和对请求的响应。没有数据显示。所以我认为我的回复格式有问题。
def stream(request):
def stream_data():
REDIS_CONF = {
'host': 'localhost',
'port': 6379,
'db': 1,
}
red = redis.StrictRedis(**REDIS_CONF)
pubsub = red.pubsub()
pubsub.subscribe('@NBA')
for message in pubsub.listen():
long_string = '''
id: 123 \n\n
data: 123123123 \n
'''
return long_string
return HttpResponse(stream_data(), content_type="text/event-stream")