--- googlecode.com/svn/trunk/public_html/stream.html 2007/12/20 21:24:24 32 +++ googlecode.com/svn/trunk/public_html/stream.html 2009/01/19 11:19:41 64 @@ -7,49 +7,34 @@ parent.Meteor.register(this); var streamreq; var byteoffset; +var newdata; function abort() { streamreq.abort(); } function newXmlHttp() { - try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} - try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} - try { return new XMLHttpRequest(); } catch(e) {} - return null; + var xhr = null; + try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { + try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { + try { xhr = new XMLHttpRequest(); } catch(e) { return false } + } + } + return xhr; } function startstream() { streamreq = newXmlHttp(); byteoffset = 0; + newdata = ""; var url = parent.Meteor.getSubsUrl(); streamreq.open("GET", url, true); streamreq.onreadystatechange = function() { if (typeof streamreq == "undefined") return; if (streamreq.readyState == 3) { - var buffer = streamreq.responseText; - var newdata = buffer.substring(byteoffset); - byteoffset = buffer.length; - while (1) { - var x = newdata.indexOf("p("); - if (x != -1) { - y = newdata.indexOf("", x); - if (y != -1) { - eval(newdata.substring((x+8),y)); - newdata = newdata.substring(y+9); - } else { - - // Last message is incomplete. Ignore it and it will be fetched again - break; - } - } else { - - // No more messages - break; - } - } - byteoffset = buffer.length-newdata.length; + extractEvents(streamreq.responseText); } else if (streamreq.readyState == 4) { + extractEvents(streamreq.responseText); delete streamreq; if (typeof(r)=="function") { r(); @@ -57,11 +42,36 @@ } } streamreq.send(null); + parent.Meteor.lastrequest = parent.Meteor.time(); } +function extractEvents(responsestr) { + newdata += responsestr.substring(byteoffset); + byteoffset = responsestr.length; + while (1) { + var x = newdata.indexOf(""); + if (x != -1) { + y = newdata.indexOf("", x); + if (y != -1) { + eval(newdata.substring((x+8),y)); + if (typeof newdata == 'undefined') break; // If message was eof() then we're now in a freed script + newdata = newdata.substring(y+9); + } else { + // Last message is incomplete. Ignore it and it will be processed next time + break; + } + } else { -startstream(); + // No more messages + break; + } + } +} + +window.onload = function() { + if (parent.Meteor.status == 4) startstream(); +} \ No newline at end of file