Comet (long polling) and XmlHttpRequest status
Asked Answered
P

2

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?

Penoyer answered 12/4, 2010 at 19:5 Comment(7)
Couldn't you just look at any existing implementation, e.g. GWT like you mentioned, jquery, etc?Delaine
@fig-gnuton: I'm not sure, if it's really easy to follow generated javascript code (I'm not a javascript guru...)Penoyer
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.Delaine
@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.Penoyer
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): https://mcmap.net/q/464638/-chrome-39-s-loading-indicator-keeps-spinning-during-xmlhttprequest/…Snowy
@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.)Penoyer
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.Penoyer
P
4

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

Penoyer answered 8/6, 2010 at 7:18 Comment(0)
O
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

Odious answered 26/6, 2013 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.