users@grizzly.java.net

Re: Dead Simple Comet example

From: Jim Driscoll <Jim.Driscoll_at_Sun.COM>
Date: Thu, 01 May 2008 20:35:08 -0700

(Taking it to users@)

Jeanfrancois Arcand wrote:
> Jim Driscoll wrote:
>>> Right, as you first need to reconnect. I know this is possible to do,
>>> but I don't know how to do that :-) I think you need to register a
>>> callback that gets invoked when the connection die.
>>
>> I don't know of a way to do that with the location object - so I'd
>> instead have to use a XMLHttpRequest object instead.
>
> Let me know what you found.

Solved it, and I feel silly. Turns out it's easy.

If the connection times out on the iframe, then an onload event will be
called. On an error loading, onerror will be called.

<iframe name="hidden" src="CometCount" frameborder="0" height="0"
width="100%" onload="restartPollOnLoad()" onerror="restartPollOnError()"
></iframe>


and

function restartPollOnLoad() {
     hidden.location = url;
}
var retries = 0;
function restartPollOnError() {
     if (retries++ > 10) {
         alert("The connection has errored out too many times");

     } else {
          hidden.location = url;
     }
}

Have to add the retry limit so you don't get a nasty case of "browser
lock" in the event of an error.

Jim