Skip to content

Instantly share code, notes, and snippets.

@Sennahoi
Created August 5, 2014 14:18
Show Gist options
  • Save Sennahoi/c695c3d2cf7163082477 to your computer and use it in GitHub Desktop.
Save Sennahoi/c695c3d2cf7163082477 to your computer and use it in GitHub Desktop.
Tomcat Comet Example
public class NIOServlet extends HttpServlet implements CometProcessor {
@Override
public void event(CometEvent event) throws IOException, ServletException {
HttpServletResponse response = event.getHttpServletResponse();
if (event.getEventType() == CometEvent.EventType.BEGIN) {
new HandlerThread(event, response).start();
} else if (event.getEventType() == CometEvent.EventType.ERROR) {
event.close();
} else if (event.getEventType() == CometEvent.EventType.END) {
event.close();
} else if (event.getEventType() == CometEvent.EventType.READ) {
// noop
}
}
}
// Send data
response.getWriter().write("<html><body>OK</body></html>");
response.getWriter().close();
// Send redirect
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", "http://www.google.de");
event.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment