--- googlecode.com/svn/trunk/public_html/meteor.js 2007/04/30 18:38:55 14 +++ trunk/public_html/meteor.js 2009/03/28 01:42:07 67 @@ -1,295 +1,348 @@ -// Set domain at highest level -var domainparts = document.domain.split("."); -document.domain = domainparts[domainparts.length-2]+"."+domainparts[domainparts.length-1]; - -Function.prototype.bind = function(obj) { - var method = this, - temp = function() { - return method.apply(obj, arguments); - }; - return temp; -} -Function.prototype.andThen=function(g) { - var f=this; - var a=this.arguments - return function(args) { - f(a);g(args); - } -}; - -function Meteor(instID) { - - this.lastmsgreceived = -1; - this.transferDoc = false; - this.pingtimer = false; - this.updatepollfreqtimer = false; - this.lastrequest = 0; - this.recvtimes = new Array(); - this.MHostId = false; - this.callback_process = function() {}; - this.callback_reset = function() {}; - this.callback_eof = function() {}; - this.callback_changemode = function() {}; - this.callback_statuschanged = function() {}; - this.persist = true; - this.frameloadtimer = false; - this.frameurl = false; - this.debugmode = false; - - // Documented public properties - this.channel = false; - this.subdomain = "data"; - this.dynamicpageaddress = "push"; - this.backtrack = 0; - this.smartpoll = true; - this.pollfreq = 2000; - this.minpollfreq = 2000; - this.mode = "poll"; - this.polltimeout=30000; - this.maxmessages=0; - this.pingtimeout = 10000; - this.status = 0; - - /* Statuses: 0 = Uninitialised, - 1 = Loading stream, - 2 = Loading controller frame, - 3 = Controller frame timeout, retrying. - 4 = Controller frame loaded and ready - 5 = Receiving data - */ - - this.instID = (typeof(instID) != "undefined") ? instID : 0; - this.MHostId = Math.floor(Math.random()*100000000)+""+this.instID; -} - -Meteor.instances = new Array(); - -Meteor.create = function(instID) { - if (!instID) instID = Meteor.instances.length; - Meteor.instances[instID] = new Meteor(instID); - return Meteor.instances[instID]; -} - -Meteor.register = function(ifr) { - instid = new String(ifr.window.frameElement.id); - instid = instid.replace(/.*_([0-9]*)$/, "$1"); - ifr.p = this.instances[instid].process.bind(this.instances[instid]); - ifr.r = this.instances[instid].reset.bind(this.instances[instid]); - ifr.eof = this.instances[instid].eof.bind(this.instances[instid]); - ifr.get = this.instances[instid].get.bind(this.instances[instid]); - ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]); - clearTimeout(this.instances[instid].frameloadtimer); - this.instances[instid].setstatus(4); - if (this.debugmode) console.log("Frame registered"); -} - -Meteor.prototype.start = function() { - this.persist = (this.maxmessages)?1:0; - this.smartpoll = (this.smartpoll)?1:0; - this.mode = (this.mode=="stream")?"stream":"poll"; - if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified"; - this.stop(); - var now = new Date(); - var t = now.getTime(); - this.setstatus(1); - if (this.mode=="stream") { - var surl = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId; - if (this.lastmsgreceived >= 0) { - surl += "&restartfrom="+this.lastmsgreceived; - } else if (this.backtrack > 0) { - surl += "&backtrack="+this.backtrack; - } else if (this.backtrack < 0 || isNaN(this.backtrack)) { - surl += "&restartfrom="; - } - this.createIframe(surl); - 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.recvtimes[0] = t; - if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer); - this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500); - } - this.lastrequest = t; -} - -Meteor.prototype.createIframe = function(url) { - if (document.all) { - this.transferDoc = new ActiveXObject("htmlfile"); - this.transferDoc.open(); - this.transferDoc.write(""); - this.transferDoc.write("