6

I'm playing around a little bit with raw XmlHttpRequestObjects + Comet Long Polling. (Usually, I'd let GWT or another framework handle of this for me, but I want to learn more about it.)

I wrote the following code:

function longPoll() {
  var xhr = createXHR(); // Creates an XmlHttpRequestObject
  xhr.open('GET', 'LongPollServlet', true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {

        if (xhr.status == 200) {
            ...
        }

        if (xhr.status > 0) {
            longPoll();
        }
    }
  }
  xhr.send(null);
}

...
<body onload="javascript:longPoll()">...

I wrapped the longPoll() call in an if statement that checks for status > 0, because I encountered, that when I leave the page (by browsing somewhere else, or by reloading it), one last unnecessary comet call is sent. [And on Firefox, it even causes severe problems when doing a page reload, for some reason I don't fully understand yet.]

Question: Is that status check the correct way to handle this problem, or is there a better solution?

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • Couldn't you just look at any existing implementation, e.g. GWT like you mentioned, jquery, etc? –  Apr 12 '10 at 19:07
  • @fig-gnuton: I'm not sure, if it's really easy to follow generated javascript code (I'm not a javascript guru...) – Chris Lercher Apr 12 '10 at 19:24
  • If you know enough to be able to play with raw XHR, you'll have no problem looking at Jquery or other libs. They have source versions that are fully commented. –  Apr 12 '10 at 19:34
  • @fig-gnuton: Okay, I haven't found this kind of source code yet - the source code I found so far, is quite complex and almost 100% undocumented. As JavaScript has the potential to drive me crazy anyway (I'm *addicted* to strong typing etc), I'd prefer a link to a javascript comet tutorial with enough depth to cover this kind of question, or maybe even a concrete answer. Or a link to some concrete location in source code at least. – Chris Lercher Apr 12 '10 at 20:22
  • I don't understand why you test for `status > 0`. After all, the `onreadystatechange` function will only be called with `readyState == 4` when a request completes. How did you see that "one last unnecessary comet call is sent"? And what severe problems in Firefox are you talking about? Perhaps the test case in another answer is helpful to you (with jQuery, but it's essentially the same): http://stackoverflow.com/questions/2703861/google-chrome-ajax/2713567#2713567 – Marcel Korpel Apr 26 '10 at 14:52
  • @Marcel: In the cases I tested, `onreadystatechange` always gets called when leaving the page. It has `readyState == 4`, but a `status == 0`. It's easy to see that by inserting a simple `alert(...)` But I don't know, if this is standard behaviour (that's part of my question.) – Chris Lercher Apr 26 '10 at 19:40
  • The severe problem in Firefox is this: If I don't insert the `if (xhr.status > 0)`, then after the page reload, the *first* Ajax call doesn't work at all. It's as if it collides with the last Ajax call that gets issued milliseconds before the page reload. – Chris Lercher Apr 26 '10 at 19:42

2 Answers2

4

My current answer - until proven false - is, that the solution is correct.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
0

i like the simplicity of this loop.... i think the server side script has to sleep or atleast loop until it gets new data before its considered long polling though this is just normal polling. i would also add something to check if the reques fails though. wrapping that in a try catch bloch should do the trick

Alex_Nabu
  • 311
  • 3
  • 11