Comet Jetty / Tomcat having some browser problems with Firefox and Chrome

I am studying the use of a comet for a project that I am working on.
First I tried to create a test application using the Tomcat6 and CometProcessor API, and then with Continuations Jetty7.
The application works on both, but I have some problems with the actual display of messages.
I used the XMLHttpRequest connection creation technique and constantly opened it so that the server can continuously transmit data to all connected clients when it is available.

My client-side code is similar to this:

function fn(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if (xhr.readyState==3){ document.getElementById('dv').innerHTML =(xhr.responseText); } if (xhr.readyState==4){ alert ('done'); } } xhr.open("GET", "First", true); xhr.send(null); } 

I found this thing to use readyState 3 somewhere on the web.

I am currently facing two problems:

  • On Firefox, this code works fine. But , if I open a new tab or even a new browser window, this will not lead to a new connection to the server, and nothing will appear on a new tab or window, only the first tab / window will receive a display. I used wireshark to test this , and only 1 connection shows it even after opening the second tab. I can’t understand why this will happen. I read about two connection restrictions, but there is only one connection here.

  • Secondly, in Chrome, the above code does not work, and the callback is not called for readstate from 3 , only when the connection closes the server I get the output.

I would also like to ask what is the best way / framework for a comet with Java. I am currently using client side jQuery.
Any suggestions would be appreciated !! Thanks

+3
firefox google-chrome jetty comet readystate
source share
3 answers

Using comets with Jetty works well with baye and dojo. higher level support than plain XMLHttpRequest. Instead, you get subscriptions to individual channels and the ability to register functions that should be triggered when certain events appear on the channel. It is very simple to have multiple connections to different tabs in one browser and works (in my experience) with Firefox, Chrome and Safari.

I have a server running in Java and clients in javascript.

0
source share

I am also unhappy with the behavior of Chrome.

My solution was to close the threads on the server after sending each response and create a new client-side request after each response received (sequential chain).

See my pure-Tomcat example: http://sublemon.com/blog/?p=10 .

0
source share

This wired behavior of Chrome is really annoying. I tried to figure out how GMail (Google’s own application) implements a comet in Chrome, but there is no suitable Http Sniffer to catch Chrome HTTP traffic forever.

Solution 1: My original thought:

We can have the Content-Type: multipart / x-mixed-replace header in the Comet Http response. I tested it. If the response is multiple , xhr.responseText is not empty if ( xhr.readyState == 3 ) is true.

The only problem is that xhr.responseText is the whole answer instead of the "substituted" answer, as Firefox does. For example, the server sends β€œA”, then β€œB” instead of β€œA”, then β€œC” to replace β€œB”. In Firefox, you get "A", "B", "C" when xhr.readyState == 4 . In Chrome, you will get "A", "AB" and "ABC" when xhr.readyState == 3.

So, your javascript client should parse xhr.responseText to retrieve the migrated data.

Solution 2: This is recommended by Safari http://lists.macosforge.org/pipermail/webkit-dev/2007-June/002041.html .

The webit engine does not display pending data until a sufficient number of bytes appear. It is claimed that an initial pad of 256 bytes is required. I tried in Chrome (4.1.249.1036 (41514)). It seems like it takes about 1 kilobyte byte for the first payload trigger (readyState == 3).

Make sure XHR is not sent directly to the onload event handler. Otherwise, the page has a download indicator in the header or URL bar.

0
source share

All Articles