java - HTML5 Server Sent Event Servlet on Tomcat 7 buffers events until socket close -


i'm trying add server side event servlet web application. below code servlet: ( several different versions )

using getoutputstream():

@webservlet(urlpatterns = {"/hello"}, asyncsupported = true) public class helloservlet extends httpservlet  {     private static final long serialversionuid = 2889150327892593198l;     public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception      {         response.setcontenttype("text/event-stream");            response.setheader("cache-control", "no-cache");         response.setheader("connection", "keep-alive");         response.setcharacterencoding("utf-8");          servletoutputstream out = response.getoutputstream();          try{                 for(int i=0; i<10; i++) {                  out.print("id: "+ +"\n");                 out.print("data: "+ system.currenttimemillis() +"\n\n");                 out.flush();                 response.flushbuffer();                  system.out.println("time sent:"+ system.currenttimemillis());                 system.out.println("iscommitted:" + response.iscommitted() );                 thread.sleep(1000);             }         }         catch(exception e)           {             e.printstacktrace();         }         out.close();         }  } 

using printstream:

public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception  {     response.setcontenttype("text/event-stream");        response.setheader("cache-control", "no-cache");     response.setheader("connection", "keep-alive");     response.setcharacterencoding("utf-8");      printwriter writer = response.getwriter();      try{         for(int i=0; i<10; i++) {              writer.write("id: "+ +"\n");             writer.write("data: "+ system.currenttimemillis() +"\n\n");             writer.flush();             response.flushbuffer();              system.out.println("time sent:"+ system.currenttimemillis());             system.out.println("iscommitted:" + response.iscommitted() );              thread.sleep(1000);         }     }     catch(exception e)       {         e.printstacktrace();     }     writer.close();  } 

an async version using https://github.com/mariomac/jeasse

ssedispatcher dispatcher = null;  @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception  {     system.out.println("test started.  async mode.");     startthread();     dispatcher = new ssedispatcher(req).ok().open(); }  private void startthread()  {     thread thread = new thread()      {          @override public void run()          {             system.out.println("thread started");             try {                 for(int i=0; i<10; i++)                  {                     system.out.println("sending message " + i);                     ongivenevent("message"+1, system.currenttimemillis()+"" ) ;                     thread.sleep(1000);                 }             } catch (exception e) {                  e.printstacktrace();             }              dispatcher.close();             system.out.println("thread ending");         }     };      thread.setdaemon(true);     thread.setname("testsse");     thread.start();     system.out.println("starting thread"); }  public void ongivenevent(string eventname, string data) throws ioexception  {     if( dispatcher != null)     {         dispatcher.send(eventname, data);     } } 

for version i'm using curl validate it:

curl -i -h "accept: application/json"  -x http://localhost:8080/testsse/hello 

the output comes @ end when connection closed server. if servlet not close connection never output.

i need events arrive after server sends them, , not when connection closed. doing wrong? cannot way supposed work.

i've googled , tried many examples , suffer same issue.

i've tried many different versions of tomcat 7.0.67, 7.0.34, 7.0.62. i'm using 64 bit version of jdk 8.0.65 on windows or jdk 8.0.45 on linux.

does have working? doing wrong?

i figured out solution this. switched using jersey sse , functions expected.

i never above solution function correctly. server buffered io until connection closed. tried kinds of flushing , manipulating buffer size.

hopefully helps... start framework such jersey sse.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -