--- googlecode.com/svn/trunk/public_html/meteor.js 2007/05/05 15:04:41 20 +++ googlecode.com/svn/trunk/public_html/meteor.js 2007/08/01 17:38:00 27 @@ -30,20 +30,23 @@ this.callback_eof = function() {}; this.callback_changemode = function() {}; this.callback_statuschanged = function() {}; - this.persist = true; + this.persist = 1; this.frameloadtimer = false; this.debugmode = false; this.subsurl = false; this.channels = {}; + this.channelcount = 0; + this.streamreq = false; + this.byteoffset = 0; // Documented public properties this.subdomain = "data"; this.dynamicpageaddress = "push"; - this.smartpoll = true; + this.smartpoll = 1; this.pollfreq = 2000; this.minpollfreq = 2000; - this.mode = "poll"; - this.polltimeout=30000; + this.mode = "stream"; + this.polltimeout = 30000; this.pingtimeout = 10000; this.maxmessages = 0; this.status = 0; @@ -93,44 +96,38 @@ if (this.debugmode) console.log("Joined channel "+channelname+", channel list follows"); if (this.debugmode) console.log(this.channels); if (this.status != 0) this.start(); + this.channelcount++; } Meteor.prototype.leaveChannel = function(channelname) { if (typeof(this.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed"; delete this.channels[channelname]; if (this.status != 0) this.start(); + this.channelcount--; } Meteor.prototype.start = function() { - this.persist = (this.maxmessages)?1:0; + this.persist = (this.persist)?1:0; this.smartpoll = (this.smartpoll)?1:0; this.mode = (this.mode=="stream")?"stream":"poll"; - if (!this.subdomain || this.channels.length) throw "Channel or Meteor subdomain host not specified"; + if (!this.subdomain || !this.channelcount) throw "Channel or Meteor subdomain host not specified"; this.stop(); var now = new Date(); var t = now.getTime(); this.setstatus(1); - var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId; - if (this.maxmessages && !this.persist) surl += "&maxmessages=" + this.maxmessages; - for (var c in this.channels) { - surl += "&channel="+c; - if (this.channels[c].lastmsgreceived >= 0) { - surl += "&restartfrom="+this.channels[c].lastmsgreceived; - } else if (this.channels[c].backtrack > 0) { - surl += "&backtrack="+this.channels[c].backtrack; - } else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) { - surl += "&restartfrom="; - } - } - this.subsurl = surl; + this.updateSubsUrl(); if (this.mode=="stream") { - this.createIframe(this.subsurl); + if (document.all) { + this.createIframe(this.subsurl); + } else { + this.createIframe("http://"+this.subdomain+"."+location.hostname+"/stream.html"); + } var f = this.pollmode.bind(this); clearTimeout(this.pingtimer); this.pingtimer = setTimeout(f, this.pingtimeout); } else { - this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html"); + this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html?nc="+t); this.recvtimes[0] = t; if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer); this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500); @@ -138,8 +135,30 @@ this.lastrequest = t; } +Meteor.prototype.updateSubsUrl = function() { + + // If streaming or long polling, connection should persist + this.persist = (this.mode == "stream" || (this.mode=='poll' && this.maxmessages > 0)) ? 1 : 0; + var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId; + if (this.persist && this.mode != "stream") surl += "&maxmessages=" + this.maxmessages; + surl += "&persist="+this.persist; + for (var c in this.channels) { + surl += "&channel="+c; + if (this.channels[c].lastmsgreceived > 0) { + surl += "&restartfrom="+(this.channels[c].lastmsgreceived+1); + } else if (this.channels[c].backtrack > 0) { + surl += "&backtrack="+this.channels[c].backtrack; + } else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) { + surl += "&restartfrom="; + } + } + this.subsurl = surl; +} + Meteor.prototype.createIframe = function(url) { - if (document.all) { + delete this.transferDoc; + if (document.all) try { this.transferDoc = new ActiveXObject("htmlfile") } catch(ex) { this.transferDoc = null } + if (document.all && this.transferDoc) { this.transferDoc = new ActiveXObject("htmlfile"); this.transferDoc.open(); this.transferDoc.write(""); @@ -161,11 +180,16 @@ ifr.style.zIndex = "-20"; ifr.setAttribute("id", "meteorframe_"+this.instID); ifr.Meteor = Meteor; - var innerifr = document.createElement("IFRAME"); - innerifr.setAttribute("src", url); - innerifr.setAttribute("id", "meteorinnerframe_"+this.instID); - ifr.appendChild(innerifr); - document.body.appendChild(ifr); + if (document.compatMode=='CSS1Compat') { + var innerifr = document.createElement("IFRAME"); + innerifr.setAttribute("src", url); + innerifr.setAttribute("id", "meteorinnerframe_"+this.instID); + ifr.appendChild(innerifr); + document.body.appendChild(ifr); + } else { + ifr.setAttribute("src", url); + document.body.appendChild(ifr); + } } if (this.debugmode) console.log("Loading URL '"+url+"' into frame..."); var f = this.frameloadtimeout.bind(this); @@ -180,9 +204,9 @@ document.getElementById("meteorframe_"+this.instID).src="about:blank"; document.body.removeChild(document.getElementById("meteorframe_"+this.instID)); } - if (!isNaN(this.pingtimer)) clearTimeout(this.pingtimer); - if (!isNaN(this.updatepollfreqtimer)) clearTimeout(this.updatepollfreqtimer); - if (!isNaN(this.frameloadtimer)) clearTimeout(this.frameloadtimer); + clearTimeout(this.pingtimer); + clearTimeout(this.updatepollfreqtimer); + clearTimeout(this.frameloadtimer); this.setstatus(0); } @@ -209,6 +233,7 @@ while (this.recvtimes.length > 5) this.recvtimes.shift(); } } + this.updateSubsUrl(); this.setstatus(5); } @@ -292,6 +317,7 @@ } } + Meteor.createCookie = function(name,value,days) { if (days) { var date = new Date(); @@ -315,4 +341,4 @@ Meteor.eraseCookie = function(name) { createCookie(name,"",-1); -} \ No newline at end of file +}