简体   繁体   中英

How to setup server side sent event with Django response

I'm in the middle of implementing server sent event using Django.

HTML

 <script type='text/javascript'>
    var source = new EventSource('/pro_listen');
    console.log(source);
    source.onmessage = function (event) {
      console.log(event.data);
    };
 </script>

and pro_listen code is here.

views

def pro_listen(request):
    return HttpResponse("{'data':'new'}", content_type="text/event-stream")

So the real problem is why console.log(event.data); doesn't print anything...

Update:

After tried with StreamingHttpReponse event.data still doesn't print anything ...

Please help me here

Django本身不支持服务器端事件和其他持久连接,因此您应使用专用的异步框架(例如Tornado)来实现此类功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM