--- googlecode.com/svn/trunk/public_html/stream.html 2007/05/14 13:42:45 21 +++ googlecode.com/svn/trunk/public_html/stream.html 2009/01/19 11:19:41 64 @@ -7,91 +7,71 @@ parent.Meteor.register(this); var streamreq; var byteoffset; - -Function.prototype.bind = function(obj) { - var method = this, - temp = function() { - return method.apply(obj, arguments); - }; - return temp; -} +var newdata; function abort() { streamreq.abort(); } function newXmlHttp() { - var xmlhttp; - /*@cc_on @*/ - /*@if (@_jscript_version >= 5) - try { - xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); - } catch (e) { - try { - xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); - } catch (E) { - xmlhttp = false; + 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 } } } - @end @*/ - if (!xmlhttp && typeof XMLHttpRequest!='undefined') { - xmlhttp = new XMLHttpRequest(); - } - return xmlhttp; + return xhr; } function startstream() { streamreq = newXmlHttp(); byteoffset = 0; - var url = get("subsurl")+"&template=2"; - var now = new Date(); - var t = now.getTime(); - url += "&nocache="+t; + newdata = ""; + var url = parent.Meteor.getSubsUrl(); streamreq.open("GET", url, true); - streamreq.onreadystatechange = handleresponse.bind(streamreq); + streamreq.onreadystatechange = function() { + if (typeof streamreq == "undefined") return; + if (streamreq.readyState == 3) { + extractEvents(streamreq.responseText); + } else if (streamreq.readyState == 4) { + extractEvents(streamreq.responseText); + delete streamreq; + if (typeof(r)=="function") { + r(); + } + } + } streamreq.send(null); + parent.Meteor.lastrequest = parent.Meteor.time(); } -function handleresponse() { - if (this.readyState == 3) { - var buffer = this.responseText; - var newdata = buffer.substring(byteoffset); - byteoffset = buffer.length; - var x = newdata.indexOf("parent.Meteor.setServerTime("); +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,y+2)); - } - 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 corrupt or incomplete. Ignore it and it will be fetched again - break; - } + 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 { - // No more messages + // Last message is incomplete. Ignore it and it will be processed next time break; } - } - byteoffset = buffer.length-newdata.length; - } else if (this.readyState == 4) { - delete streamreq; - if (typeof(startstream)=="function") { - startstream(); - } else if (typeof(r)=="function") { - r(); + } else { + + // No more messages + break; } } } -startstream(); +window.onload = function() { + if (parent.Meteor.status == 4) startstream(); +} \ No newline at end of file