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
。 你能帮我吗?
I've this class:
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);
}
}
}
But I can't catch event.getEventSubType() == EventSubType.CLIENT_DISCONNECT
.
Can you help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎 CLIENT_DISCONNECT 仅使用 ERROR 的子类型,并且当从客户端关闭套接字时会发生这种情况。
it seems that CLIENT_DISCONNECT is only used a subtype of ERROR and that happens when the socket is closed from the client side.