日期:2014-05-19 浏览次数:20692 次
public class FightCometServlet extends HttpServlet implements CometProcessor{ private static Log log = LogFactory.getLog(FightCometServlet.class); private static final Integer TIMEOUT = 60 * 1000; private FightLogic fightLogic; @Override public void init() throws ServletException { ServletContext servletContext = this.getServletContext(); this.fightLogic = (FightLogic)WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("fightLogic"); } public void event(final CometEvent event) throws IOException, ServletException { HttpServletRequest request = event.getHttpServletRequest(); HttpServletResponse response = event.getHttpServletResponse(); response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); HttpSession session = request.getSession(true); PrintWriter writer = response.getWriter(); Message message = null; if (event.getEventType() == CometEvent.EventType.BEGIN) { writer.println("什么什么"); log.info("Writing:" + messageStr); writer.flush(); writer.close(); } else if (event.getEventType() == CometEvent.EventType.ERROR) { log.info("Error for session: " + request.getSession(true).getId()); event.close(); } else if (event.getEventType() == CometEvent.EventType.END) { log.info("End for session: " + request.getSession(true).getId()); event.close(); } else if (event.getEventType() == CometEvent.EventType.READ) { throw new UnsupportedOperationException("This servlet does not acceptdata"); } } }