I am currently writing a Comet application which requires me to send chunks of data at a time on a persistent connection. However, I’m having trouble flushing the message to the client before closing the connection. Is there any reason the PrintWriter.flush() method is not behaving like I think it should?
This is my Tomcat Comet implementation:
public void event(CometEvent event) throws IOException, ServletException {
HttpServletRequest request = event.getHttpServletRequest();
HttpServletResponse response = event.getHttpServletResponse();
if (event.getEventType() == EventType.BEGIN) {
request.setAttribute("org.apache.tomcat.comet.timeout", 300 * 1000);
PrintWriter out = response.getWriter();
out.println("BEGIN!");
out.flush();
System.out.println("EventType.BEGIN");
} else if (event.getEventType() == EventType.READ) {
InputStream is = request.getInputStream();
byte[] buf = new byte[512];
do {
int n = is.read(buf); //can throw an IOException
if (n > 0) {
System.out.println("Read " + n + " bytes: " + new String(buf, 0, n)
+ " for session: " + request.getSession(true).getId());
} else if (n < 0) {
return;
}
} while (is.available() > 0);
System.out.println("subtype: "+event.getEventSubType());
System.out.println("EventType.READ");
} else if (event.getEventType() == EventType.END) {
PrintWriter out = response.getWriter();
out.println("END!");
out.close();
System.out.println("checkError: "+out.checkError());
System.out.println(event.getEventSubType());
System.out.println("EventType.END");
//eventWorker.enqueue(new EndEvent(request, response));
} else if (event.getEventType() == EventType.ERROR) {
PrintWriter out = response.getWriter();
out.println("ERROR!");
out.flush();
System.out.println("checkError: "+out.checkError());
System.out.println("subtype: "+event.getEventSubType());
//response.getWriter().close();
System.out.println("EventType.ERROR");
} else {
(new ServletException("EXCEPTION")).printStackTrace();
}
}
So here I’m trying to send the message “BEGIN!” and keep the connection open afterwards so I can send more data. However, it seems that the message doesn’t go through until the connection is closed.
This is my ajax code:
$.post(‘comet’, function(data) { alert(data); });
After I run this code, Firebug tells me that this is the response header:
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Date: Mon, 13 Jul 2009 21:16:29 GMT
This leads me to think that my browser received some data back, but how do I update something on the page before the connection is closed?
So it seems that the browser was receiving data this whole time, but because the connection wasn’t closed, the JavaScript thought data was still being sent. This is why my jQuery callback function was not being called.
Looking at the W3C AJAX tutorial, I noticed that there are different ready states for the XMLHttpRequest object.
Traditionally, people only handle the 4th readyState, which means transfer is complete. In my case, however, I needed to read data before the transfer ends. Hence, I needed code to handle the 3rd readyState.