views:

511

answers:

2

Trying to get my way trought Comet with Java servlets, I encountered a big problem: There seems to be no way to use the established connection to the client to send the server additional data from the browser (works in plain java when writing to the inputstream).

Following problem arises for a CometChat application when a Client connects to servlet, receives a form for sending input and a form for presenting server output: Now if the client wants to send some data at this connection, resulting in a READ event at the servlet, how can this be done?

I tried sending GET, HEAD and POST. With HEAD the comet connection is closed afterwards. GET always produces END, BEGIN and POST produces BEGIN, READ.

I tried searching the web, but the only answer I found was: Comet READ events are generating when there is a POST method with a body

How can I achieve this?

I'm using plain Javascript Ajax:

function send(content) {
   var text = document.controller.input.value;
   params = 'input=' + content;
   var ajaxObj = createXMLHttp();
   ajaxObj.open('POST', 'CometChat', true);
   ajaxObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   ajaxObj.setRequestHeader('Content-Length', params.length); 
   ajaxObj.setRequestHeader('Connection', 'close');
   ajaxObj.onreadystatechange = function() {};
   ajaxObj.send(params);
}

This produces BEGIN, READ. What headers do I need to set to produce a solely READ event only?

I'm able to 'cheat' this by looking up my connections and reuse response, but on client side, the AJAX request stays in interactive mode (although flushing it on the server) and I'm only able to may 5 requests on FF and 10 requests on IE before the following request is not processed. Also as soon as the first AJAX request is received on the server, I'm get TIMEOUT events, two per request repeating forever.

What's the real way?

+1  A: 

Good luck, creating a Comet app with Java Servlets is a pretty complicated undertaking. Plus Tomcat wasn't really designed for it. I suggest you check out StreamHub Comet Server.

rajax
Yes, observed this too, just wanted to learn technology. But why does Tomcat support this features when it is not usable and not the way to go??? I stumled upon StreamHub Comet Server and most likely this will be my way to go. Another option might be Project Atmosphere from Glassfish that claims to support all AppServers plus Tomcat and Jetty. But thanks, your answer gives me a point to forget about pure tomcat comet development... Greetz, GHad
GHad
No problem. I think Comet was getting popular at the time - so they decided to tack it on without really thinking about it. For truly performant Comet you need a server written to handle the large number of open connections. Atmosphere, or Grizzly might be another option.
rajax
A: 

As rajax says, developing a Comet app in servlets is a really bad idea.

photographique