01: package example;
02:
03: import java.io.*;
04: import java.util.*;
05: import java.util.concurrent.*;
06:
07: import javax.servlet.*;
08: import javax.servlet.http.*;
09:
10: import javax.webbeans.In;
11:
12: import com.caucho.servlet.comet.*;
13:
14: public class TestCometServlet extends GenericCometServlet {
15: @In
16: private TimerService _timerService;
17:
18: private ArrayList<CometState> _itemList = new ArrayList<CometState>();
19:
20: @Override
21: public boolean service(ServletRequest request,
22: ServletResponse response, CometController controller)
23: throws IOException, ServletException {
24: HttpServletRequest req = (HttpServletRequest) request;
25: HttpServletResponse res = (HttpServletResponse) response;
26:
27: PrintWriter out = res.getWriter();
28: res.setHeader("Cache-Control", "no-cache, must-revalidate");
29: res.setHeader("Expires", "Mon, 27 Jul 1997 05:00:00 GMT");
30:
31: res.setContentType("text/html");
32:
33: out.println("<html><body>");
34:
35: // Padding needed because Safari needs at least 1k data before
36: // it will start progressive rendering.
37: out.print("<!--");
38: for (int i = 0; i < 1024; i++) {
39: if (i % 64 == 0)
40: out.println();
41:
42: out.print("*");
43: }
44: out.println();
45: out.println("-->");
46:
47: out.println("<script type='text/javascript'>");
48: out.println("var comet_update = window.parent.comet_update;");
49: out.println("</script>");
50:
51: CometState state = new CometState(controller);
52:
53: // Add the comet state to the controller
54: _timerService.addCometState(state);
55:
56: return true;
57: }
58:
59: @Override
60: public boolean resume(ServletRequest request,
61: ServletResponse response, CometController controller)
62: throws IOException, ServletException {
63: HttpServletRequest req = (HttpServletRequest) request;
64: HttpServletResponse res = (HttpServletResponse) response;
65:
66: PrintWriter out = res.getWriter();
67:
68: Object count = controller.getAttribute("comet.count");
69:
70: out.println("<script type='text/javascript'>");
71: out.println("comet_update(" + count + ");");
72: out.println("</script>");
73:
74: return true;
75: }
76: }
|