我有這個類:Comet和NIO - 客戶端斷開
public class CometServlet extends HttpServlet implements CometProcessor{
private static final Integer TIMEOUT = 20 * 1000;
@Override
public void event(final CometEvent event) throws IOException, ServletException {
HttpServletRequest request = event.getHttpServletRequest();
HttpServletResponse response = event.getHttpServletResponse();
if (event.getEventType() == CometEvent.EventType.BEGIN) {
request.setAttribute("org.apache.tomcat.comet.timeout", TIMEOUT);
System.out.println("Begin for session: " + request.getSession(true).getId());
} else if (event.getEventType() == CometEvent.EventType.ERROR) {
System.out.println("Error for sessions: " + request.getSession(true).getId());
if(event.getEventSubType() == EventSubType.TIMEOUT) {
System.out.println("timeout for session: " + request.getSession(true).getId());
}
else if(event.getEventSubType() == EventSubType.IOEXCEPTION) {
System.out.println("ioexception for session: " + request.getSession(true).getId());
}
else if(event.getEventSubType() == EventSubType.CLIENT_DISCONNECT) {
System.out.println("client disconnect for session: " + request.getSession(true).getId());
}
response.getWriter().close();
event.close();
} else if (event.getEventType() == CometEvent.EventType.END) {
System.out.println("End for session: " + request.getSession(true).getId());
if(event.getEventSubType() == EventSubType.WEBAPP_RELOAD) {
System.out.println("webappreload for session: " + request.getSession(true).getId());
}
else if(event.getEventSubType() == EventSubType.SESSION_END) {
System.out.println("sessionend for session: " + request.getSession(true).getId());
}
response.getWriter().close();
event.close();
} else if (event.getEventType() == CometEvent.EventType.READ) {
InputStream is = request.getInputStream();
byte[] buf = new byte[512];
do {
int n = is.read(buf); //can throw an IOException
if (n > 0) {
log("Read " + n + " bytes: " + new String(buf, 0, n)
+ " for session: " + request.getSession(true).getId());
} else if (n < 0) {
System.out.println("error(event, request, response)");
return;
}
} while (is.available() > 0);
}
}
}
但我不能趕上event.getEventSubType() == EventSubType.CLIENT_DISCONNECT
。 你能幫我嗎?
你是如何試圖觸發它? – Andrew 2011-04-21 13:57:28
我有一個移動java客戶端,提交一個簡單的獲取請求,並等待服務器的響應。但我必須管理網絡斷開連接,並且我無法捕獲此子類型事件。 – CeccoCQ 2011-04-21 14:24:42