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?
- 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, theonreadystatechange
function will only be called withreadyState == 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
2 Answers
Reset to default 4My 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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435437a4627585.html
评论列表(0条)