--- googlecode.com/svn/trunk/public_html/stream.html 2007/12/20 21:24:24 32 +++ googlecode.com/svn/trunk/public_html/stream.html 2008/02/28 00:21:39 55 @@ -7,6 +7,7 @@ parent.Meteor.register(this); var streamreq; var byteoffset; +var newdata; function abort() { streamreq.abort(); @@ -22,34 +23,15 @@ 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(); @@ -59,6 +41,29 @@ streamreq.send(null); } +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)); + newdata = newdata.substring(y+9); + } else { + + // Last message is incomplete. Ignore it and it will be processed next time + break; + } + } else { + + // No more messages + break; + } + } +} + startstream();