Hi,

I think you have to add

cometEvent.getHttpServletResponse().getWriter().flush();

also in the READ event case.

Matthias

> -----Original Message-----
> From: Peter Warren [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, May 22, 2007 10:11 PM
> To: users@tomcat.apache.org
> Subject: comet read event
> 
> My BEGIN block in my comet servlet now looks like the code 
> below (added
> a while loop to read until the buffer is empty).  Is that what you had
> in mind?  The buffer in the BEGIN event only contains the 
> client's first
> message.  Am I not emptying the buffer correctly?  Although, 
> I wouldn't
> expect the buffer to contain the client's second message since the
> client blocks for an ack from the server before sending its second
> message.  Any other thoughts on what happens to the client's second
> message and why no READ event is generated?
> 
> Thanks for your help,
> Peter
> 
>         if (cometEvent.getEventType() == CometEvent.EventType.BEGIN) {
>             log("Begin for session: " + 
> request.getSession(true).getId());
>             BufferedReader reader =
> cometEvent.getHttpServletRequest().getReader();
>             String line = null;
>             while ((line = reader.readLine()) != null) {
>                 log("servlet received: " + line);
>                
> cometEvent.getHttpServletResponse().getWriter().println("servlet
> received: " + line);
>                 
> cometEvent.getHttpServletResponse().getWriter().flush();
>             }
>         }
> 
> > Filip Hanik wrote:
> >
> > it could be because the data from the request already came 
> in with the
> request.
> > when the BEGIN happens, perform the actions as if there was 
> a READ as
> well, ie, empty out the buffer.
> >
> > Filip
> 
> Peter Warren wrote:
> > The following client code generates a comet BEGIN event on 
> the server
> > but not a subsequent READ event, as I was expecting.  How 
> come?  Is my
> > code wrong?  Are my expectations wrong?  See sequence of events
> > commented in code below.
> >
> >     // client test method that sends messages to server and 
> listens for
> > responses
> >     public void test() throws IOException {
> >         out.println("test 1");
> >         out.flush();
> >
> >         // server receives client's message, generates a 
> BEGIN event,
> > and sends response to client
> >
> >         in = new BufferedReader(new
> > InputStreamReader(urlConn.getInputStream()));
> >         System.out.println(in.readLine());
> >
> >         // client receives server's response and prints it
> >
> >         out.println("test 2");
> >         out.flush();
> >
> >         System.out.println(in.readLine());
> >
> >         // client code blocks here waiting for server response.
> >         // server never generates a READ event.  How come?
> >         // Does the HttpURLConnection (see full code below) 
> need to be
> > set up differently?
> >         // Am I using the PrintWriter incorrectly when 
> sending to the
> > comet servlet?
> >
> >         out.close();
> >         urlConn.disconnect();
> >     }
> >
> > Thanks for any help,
> > Peter
> >
> > -- system --
> >
> > using:
> > tomcat 6.0.13 on windows xp sp 2
> > java 1.6.0_01
> >
> > -- test client & comet servlet source below --
> >
> > ## begin test client ##
> >
> > import java.io.BufferedReader;
> > import java.io.IOException;
> > import java.io.InputStreamReader;
> > import java.io.PrintWriter;
> > import java.net.HttpURLConnection;
> > import java.net.URL;
> >
> > public class CometTestClient {
> >
> >     private HttpURLConnection urlConn;
> >
> >     private PrintWriter out;
> >
> >     private BufferedReader in;
> >
> >     public static void main(String[] args) throws Exception {
> >         CometTestClient test = new CometTestClient();
> >         test.test();
> >     }
> >
> >     public CometTestClient() throws IOException {
> >         initConnection();
> >     }
> >
> >     private void initConnection() throws IOException {
> >         URL url = new URL("http://127.0.0.1/CometTest";);
> >         urlConn = (HttpURLConnection) url.openConnection();
> >         urlConn.setDoInput(true);
> >         urlConn.setDoOutput(true);
> >         urlConn.connect();
> >         out = new PrintWriter(urlConn.getOutputStream());
> >     }
> >
> >     public void test() throws IOException {
> >         out.println("test 1");
> >         out.flush();
> >
> >         in = new BufferedReader(new
> > InputStreamReader(urlConn.getInputStream()));
> >         System.out.println(in.readLine());
> >
> >         out.println("test 2");
> >         out.flush();
> >
> >         System.out.println(in.readLine());
> >
> >         out.close();
> >         urlConn.disconnect();
> >     }
> > }
> >
> > ## end test client ##
> >
> > ## begin comet servlet ##
> >
> > import java.io.BufferedReader;
> > import java.io.IOException;
> >
> > import javax.servlet.ServletException;
> > import javax.servlet.http.HttpServlet;
> > import javax.servlet.http.HttpServletRequest;
> >
> > import org.apache.catalina.CometEvent;
> > import org.apache.catalina.CometProcessor;
> >
> > public class CometTestServlet extends HttpServlet implements
> > CometProcessor {
> >     private static final long serialVersionUID = 
> 5472498184127924791L;
> >
> >     public void event(CometEvent cometEvent) throws IOException,
> > ServletException {
> >         HttpServletRequest request = 
> cometEvent.getHttpServletRequest();
> >         // don't want timeout events
> >         cometEvent.setTimeout(1000000);
> >         if (cometEvent.getEventType() == 
> CometEvent.EventType.BEGIN) {
> >             log("Begin for session: " +
> > request.getSession(true).getId());
> >             BufferedReader reader =
> > cometEvent.getHttpServletRequest().getReader();
> >             String line = reader.readLine();
> >             if (line != null) {
> >                 log("servlet received: " + line);
> >               
> > cometEvent.getHttpServletResponse().getWriter().println("servlet
> > received: " + line);
> >                 
> cometEvent.getHttpServletResponse().getWriter().flush();
> >             } else {
> >                 cometEvent.close();
> >             }
> >         } else if (cometEvent.getEventType() ==
> > CometEvent.EventType.ERROR) {
> >             log("Error for session: " + 
> request.getSession(true).getId()
> > + ", " + cometEvent.getEventSubType());
> >             cometEvent.close();
> >         } else if (cometEvent.getEventType() ==
> > CometEvent.EventType.END) {
> >             log("End for session: " + 
> request.getSession(true).getId());
> >             cometEvent.close();
> >         } else if (cometEvent.getEventType() ==
> > CometEvent.EventType.READ) {
> >             log("Read for session: " + 
> request.getSession(true).getId());
> >             BufferedReader reader =
> > cometEvent.getHttpServletRequest().getReader();
> >             String line = reader.readLine();
> >             if (line != null) {
> >               
> > cometEvent.getHttpServletResponse().getWriter().println("servlet
> > received: " + line);
> >             } else {
> >                 cometEvent.close();
> >             }
> >         }
> >     }
> > }
> >
> > ## end comet servlet ##
> >
> > 
> ---------------------------------------------------------------------
> > To start a new topic, e-mail: users@tomcat.apache.org
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
> >   
> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to