samedi 28 février 2015

JS EventSource receiving mysterious error from my 3.0 async servlet



My EventSource connects, the output gets printed, but when I call asyncContext.complete() my JS EventSource receives an error. The errors for EventSource never bear a message.


For those who voted to close: It would be nice if you provided a reason next time. I even posted link to the Git project. What else is one supposed to do? o.O


Tested with Tomcat 7.0.30, 7.0.59, 8.0.20


The response returns with http status OK/200 and following headers:



  • Connection: close

  • Content-Type: text/event-stream;charset=UTF-8

  • Date: Sat, 28 Feb 2015 21:32:35 GMT

  • Server: Apache-Coyote/1.1

  • Transfer-Encoding: identity


In case anyone would be willing to try here's the minimized Maven project: BitBucket.org


I tried things like turning my antivirus SW off.


Can you see anything suspicious here, please?


JS:



function setupEventSource() {
var es = new EventSource('/sse');
es.onerror = function(event) {
console.log('[EventSource] Error while connecting to ' + es.url);
// es.close();
};
}
setupEventSource();


Java:



@WebServlet(urlPatterns = { "/sse" }, asyncSupported = true)
public class EventSource2 extends HttpServlet {
private static ScheduledExecutorService executor = Executors
.newScheduledThreadPool(10);

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");

final AsyncContext asyncContext = request.startAsync();

Runnable runnableDummy = new Runnable() {
@Override
public void run() {
try {
PrintWriter writer = asyncContext.getResponse().getWriter();
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
writer.println("data: Hello " + i + "\n");
writer.flush();
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}

asyncContext.complete();
}
};

executor.execute(runnableDummy);
}

@Override
public void destroy() {
executor.shutdown();
}
}


server.xml



<Connector port="80" protocol="HTTP/1.1"
URIEncoding="UTF-8" connectionTimeout="20000" asyncTimeout="20000" />



Aucun commentaire:

Enregistrer un commentaire