问题
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