how to implement server-sent-events in JEE6

本秂侑毒 提交于 2019-12-13 03:57:33

问题


In PHP it really simple: I just added this to "b.php":

header('Content-Type: text/event-stream');
echo "data: Hi\n\n";
flush();

and this to "a.html":

var source = new EventSource("b.php");
source.onmessage = function (event) {
document.body.innerHTML += event.data + "<br>";

I just need to go a.html and it's works!

But in java I've tried to create servlet and in doGet I added this code:

response.setContentType("text/event-stream");

PrintWriter out = response.getWriter();  
out.println("data: Hi\n\n") ;
out.flush();

And when I go to a.html (same as above) I can see that doGet is indeed invoked (in debugger) but the "onmessage" event is never happened.

How to start with SSE in JEE6? somebody can write me an example that works?

Thanks.


回答1:


checkout https://github.com/oliverwehrens/Server-Sent-Events-Java-Magazin . This is an example code to get you going.



来源:https://stackoverflow.com/questions/8958098/how-to-implement-server-sent-events-in-jee6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!