private void writeData(CometConnection c, CometMessage msg) throws IOException { c.setDyingTime(System.currentTimeMillis()); PrintWriter writer; HttpServletResponse response = c.getResponse(); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); writer = response.getWriter(); writer.print(CometProtocol.encode(msg)); close(c); }
/** * 为一个连接发送一条消息 * * @param c * @param e */ synchronized void sendTo(CometConnection c, CometMessage msg) { if (c == null) { return; } if (CometProtocol.STATE_DYING.equals(c.getState()) || c.getResponse() == null) { cacher.push(c, msg); return; } try { writeData(c, msg); } catch (IOException e1) { e1.printStackTrace(); } }
private void close(CometConnection c) throws IOException { if (c.getResponse() != null) { if (c.getWorkStyle().equals(CometProtocol.WORKSTYLE_LLOOP)) { c.setState(CometProtocol.STATE_DYING); try { c.getResponse().getWriter().close(); } catch (Exception e) { } c.setResponse(null); } else { try { c.getResponse().flushBuffer(); } catch (Exception e) { } } } }