I just learned about comet pattern(Long polling) and very curious to implement a simple example with Tomcat6 as container. What i am trying to do here is - I am going to call a servlet that generates a random number between 1 and 20 and based on that random number it should waits that number of seconds before responding to client.
For Example : if generated random number is 10 , then the servlet should wait 10 seconds before it send response.
Here is my servlet that implements CometProcessor
public class RandomNumberGenerator extends HttpServlet implements CometProcessor {
doPost method :
PrintWriter out =response.getWriter();
Random r = new Random();
int randomInt = r.nextInt(20) + 1;
out.print(randomInt);
out.flush();
out.close();
Now, as i implemented CometProcessor
I've got an event method :
@Override
public void event(CometEvent event) throws IOException, ServletException {
}
What i can't figure out is that, what Iv'e to do with this event
method to do what i want.
What i know here is, their will be some events i can handle inside this method like : BRGIN,READ,END,ERROR
Should i use events like this ?
if (event.getEventType() == CometEvent.EventType.BEGIN) {
event.setTimeout(numberOfSecondToWait);
}
I am totally confused as this tutorial seem a bit complex to me.
I recently wrote a blog post on WebSockets and Comet and asked the inventor of 'Comet' to provide his opinion.
In summary, Alex said WebSockets replace Comet.
So, although people are still using Comet it will ultimately be replaced with WebSockets since they were specifically created to solve a problem that Comet solutions hacked around.
I'd recommend you shift your focus to WebSockets.