最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Comet (long polling) and XmlHttpRequest status - Stack Overflow

programmeradmin3浏览0评论

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 et 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?

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 et 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?

Share Improve this question edited Apr 12, 2010 at 19:22 Chris Lercher asked Apr 12, 2010 at 19:05 Chris LercherChris Lercher 37.8k20 gold badges102 silver badges135 bronze badges 7
  • Couldn't you just look at any existing implementation, e.g. GWT like you mentioned, jquery, etc? – user65663 Commented Apr 12, 2010 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 Commented Apr 12, 2010 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 mented. – user65663 Commented Apr 12, 2010 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 plex 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 et 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 Commented Apr 12, 2010 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 pletes. How did you see that "one last unnecessary et 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): stackoverflow./questions/2703861/google-chrome-ajax/… – Marcel Korpel Commented Apr 26, 2010 at 14:52
 |  Show 2 more ments

2 Answers 2

Reset to default 4

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

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

发布评论

评论列表(0)

  1. 暂无评论