Comet 和 NIO - 客户端断开连接

发布于 2024-11-02 16:16:31 字数 2606 浏览 1 评论 0原文

我有这个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2024-11-09 16:16:31

似乎 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文