/[meteor]/googlecode.com/svn/trunk/public_html/meteor.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /googlecode.com/svn/trunk/public_html/meteor.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by andrew.betts, Mon Nov 20 17:59:30 2006 UTC revision 14 by andrew.betts, Mon Apr 30 18:38:55 2007 UTC
# Line 1  Line 1 
1  // Set domain at highest level  // Set domain at highest level
2  var domainparts = document.domain.split(".");  var domainparts = document.domain.split(".");
3  document.domain = domainparts[domainparts.length-2]+"."+domainparts[domainparts.length-1];  document.domain = domainparts[domainparts.length-2]+"."+domainparts[domainparts.length-1];
4    
5  Function.prototype.bind = function(obj) {  Function.prototype.bind = function(obj) {
6          var method = this,          var method = this,
7          temp = function() {          temp = function() {
8                  return method.apply(obj, arguments);                  return method.apply(obj, arguments);
9          };          };
10          return temp;          return temp;
11  }  }
12  Function.prototype.andThen=function(g) {  Function.prototype.andThen=function(g) {
13          var f=this;          var f=this;
14          var a=this.arguments          var a=this.arguments
15          return function(args) {          return function(args) {
16                  f(a);g(args);                  f(a);g(args);
17          }          }
18  };  };
19    
20  function Meteor(instID) {  function Meteor(instID) {
21    
22          this.lastmsgreceived = -1;          this.lastmsgreceived = -1;
23          this.transferDoc = false;          this.transferDoc = false;
24          this.pingtimer = false;          this.pingtimer = false;
25          this.updatepollfreqtimer = false;          this.updatepollfreqtimer = false;
26          this.lastrequest = 0;          this.lastrequest = 0;
27          this.recvtimes = new Array();          this.recvtimes = new Array();
28          this.MHostId = false;          this.MHostId = false;
29          this.callback_process = function() {};          this.callback_process = function() {};
30          this.callback_reset = function() {};          this.callback_reset = function() {};
31          this.callback_eof = function() {};          this.callback_eof = function() {};
32          this.callback_changemode = function() {};          this.callback_changemode = function() {};
33          this.persist = true;          this.callback_statuschanged = function() {};
34            this.persist = true;
35          // Documented public properties          this.frameloadtimer = false;
36          this.channel = false;          this.frameurl = false;
37          this.subdomain = "data";          this.debugmode = false;
38          this.dynamicpageaddress = "push";  
39          this.backtrack = 0;          // Documented public properties
40          this.smartpoll = true;          this.channel = false;
41          this.pollfreq = 2000;          this.subdomain = "data";
42          this.minpollfreq = 2000;          this.dynamicpageaddress = "push";
43          this.mode = "stream";          this.backtrack = 0;
44          this.polltimeout=30000;          this.smartpoll = true;
45          this.maxmessages=0;          this.pollfreq = 2000;
46          this.pingtimeout = 10000;          this.minpollfreq = 2000;
47            this.mode = "poll";
48          // Set or retrieve host id.  Cookie takes this form:          this.polltimeout=30000;
49          // MeteorID=123:6356353/124:098320454;          this.maxmessages=0;
50          var MeteIds = Meteor.readCookie("MeteorID");          this.pingtimeout = 10000;
51          var regex1 = new RegExp("^([0-9\:\/M]+\/)*"+instID+"\:([^\/]+)(\/[0-9\:\/M]+)*$");          this.status = 0;
52          var regex2 = new RegExp("^([0-9\:\/M]+\/)*M\:([^\/]+)(\/[0-9\:\/M]+)*$");  
53          if (typeof(instID) == "Number" && regex1.exec(MeteIds)) {          /* Statuses:    0 = Uninitialised,
54                  this.MHostId = ma[2];                                          1 = Loading stream,
55          } else if (typeof(instID) == "Number") {                                          2 = Loading controller frame,
56                  this.MHostId = Math.floor(Math.random()*1000000);                                          3 = Controller frame timeout, retrying.
57                  var newcookie = (MeteIds)?MeteIds+"/":"";                                          4 = Controller frame loaded and ready
58                  newcookie += instID+":"+this.MHostId;                                          5 = Receiving data
59                  Meteor.createCookie("MeteorID", newcookie);          */
60          } else if (ma = regex2.exec(MeteIds)) {  
61                  this.MHostId = ma[2];          this.instID = (typeof(instID) != "undefined") ? instID : 0;
62          } else {          this.MHostId = Math.floor(Math.random()*100000000)+""+this.instID;
63                  this.MHostId = Math.floor(Math.random()*1000000);  }
64                  var newcookie = (MeteIds)?MeteIds+"/":"";  
65                  newcookie += "M:"+this.MHostId;  Meteor.instances = new Array();
66                  Meteor.createCookie("MeteorID", newcookie);  
67          }  Meteor.create = function(instID) {
68          this.instID = (typeof(instID) != "undefined") ? instID : 0;          if (!instID) instID = Meteor.instances.length;
69  }          Meteor.instances[instID] = new Meteor(instID);
70            return Meteor.instances[instID];
71  Meteor.instances = new Array();  }
72  Meteor.servertimeoffset = 0;  
73    Meteor.register = function(ifr) {
74  Meteor.create = function(instID) {          instid = new String(ifr.window.frameElement.id);
75          if (!instID) instID = 0;          instid = instid.replace(/.*_([0-9]*)$/, "$1");
76          Meteor.instances[instID] = new Meteor(instID);          ifr.p = this.instances[instid].process.bind(this.instances[instid]);
77          return Meteor.instances[instID];          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);
78  }          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);
79            ifr.get = this.instances[instid].get.bind(this.instances[instid]);
80  Meteor.register = function(ifr) {          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);
81          instid = new String(ifr.window.frameElement.id);          clearTimeout(this.instances[instid].frameloadtimer);
82          instid = instid.replace("meteorframe_", "");          this.instances[instid].setstatus(4);
83          ifr.p = this.instances[instid].process.bind(this.instances[instid]);          if (this.debugmode) console.log("Frame registered");
84          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);  }
85          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);  
86          ifr.get = this.instances[instid].get.bind(this.instances[instid]);  Meteor.prototype.start = function() {
87          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);          this.persist = (this.maxmessages)?1:0;
88  }          this.smartpoll = (this.smartpoll)?1:0;
89            this.mode = (this.mode=="stream")?"stream":"poll";
90  Meteor.setServerTime = function(timestamp) {          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";
91          var now = new Date();          this.stop();
92          var clienttime = (now.getTime() / 1000);          var now = new Date();
93          Meteor.servertimeoffset = timestamp - clienttime;          var t = now.getTime();
94  }          this.setstatus(1);
95            if (this.mode=="stream") {
96  Meteor.prototype.start = function() {                  var surl = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId;
97          this.persist = (this.maxmessages)?1:0;                  if (this.lastmsgreceived >= 0) {
98          this.smartpoll = (this.smartpoll)?1:0;                          surl += "&restartfrom="+this.lastmsgreceived;
99          this.mode = (this.mode=="stream")?"stream":"poll";                  } else if (this.backtrack > 0) {
100          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";                          surl += "&backtrack="+this.backtrack;
101          var now = new Date();                  } else if (this.backtrack < 0 || isNaN(this.backtrack)) {
102          var t = now.getTime();                          surl += "&restartfrom=";
103          if (typeof(this.transferDoc)=="object") {                  }
104                  this.transferDoc.open();                  this.createIframe(surl);
105                  this.transferDoc.close();                  var f = this.pollmode.bind(this);
106                  delete this.transferDoc;                  clearTimeout(this.pingtimer);
107          }                  this.pingtimer = setTimeout(f, this.pingtimeout);
108          if (document.getElementById("meteorframe_"+this.instID)) {  
109                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));          } else {
110          }                  this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html");
111          if (this.mode=="stream") {                  this.recvtimes[0] = t;
112                  if (document.all) {                  if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);
113                          this.transferDoc = new ActiveXObject("htmlfile");                  this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);
114                          this.transferDoc.open();          }
115                          this.transferDoc.write("<html>");          this.lastrequest = t;
116                          this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");  }
117                          this.transferDoc.write("</html>");  
118                          var selfref = this;  Meteor.prototype.createIframe = function(url) {
119                          this.transferDoc.parentWindow.Meteor = Meteor;          if (document.all) {
120                          this.transferDoc.close();                  this.transferDoc = new ActiveXObject("htmlfile");
121                          var ifrDiv = this.transferDoc.createElement("div");                  this.transferDoc.open();
122                          this.transferDoc.appendChild(ifrDiv);                  this.transferDoc.write("<html>");
123                          var url = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId;                  this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
124                          if (this.lastmsgreceived >= 0) {                  this.transferDoc.write("</html>");
125                                  url += "&restartfrom="+this.lastmsgreceived;                  var selfref = this;
126                          } else if (this.backtrack > 0) {                  this.transferDoc.parentWindow.Meteor = Meteor;
127                                  url += "&backtrack="+this.backtrack;                  this.transferDoc.close();
128                          } else if (this.backtrack < 0 || isNaN(this.backtrack)) {                  var ifrDiv = this.transferDoc.createElement("div");
129                                  url += "&restartfrom=";                  this.transferDoc.appendChild(ifrDiv);
130                          }                  ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"\" style=\"display: none;\"></iframe>";
131                          ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"&nocache="+t+"\" style=\"display: none;\"></iframe>";          } else {
132                  } else {                  var ifr = document.createElement("IFRAME");
133                          var ifr = document.createElement("IFRAME");                  ifr.style.width = "10px";
134                          ifr.style.width = "10px";                  ifr.style.height = "10px";
135                          ifr.style.height = "10px";                  ifr.style.border = "none";
136                          ifr.style.border = "none";                  ifr.style.position = "absolute";
137                          ifr.style.position = "absolute";                  ifr.style.top = "-10px";
138                          ifr.style.top = "-10px";                  ifr.style.marginTop = "-10px";
139                          ifr.style.marginTop = "-10px";                  ifr.style.zIndex = "-20";
140                          ifr.style.zIndex = "-20";                  ifr.setAttribute("id", "meteorframe_"+this.instID);
141                          ifr.id = "meteorframe_"+this.instID;                  ifr.Meteor = Meteor;
142                          document.body.appendChild(ifr);                  var innerifr = document.createElement("IFRAME");
143                          ifr.src = "http://"+this.subdomain+"."+location.hostname+"/stream.html?nocache="+t;                  innerifr.setAttribute("src", url);
144                  }                  innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
145                  var f = this.pollmode.bind(this);                  ifr.appendChild(innerifr);
146                  clearTimeout(this.pingtimer);                  document.body.appendChild(ifr);
147                  this.pingtimer = setTimeout(f, this.pingtimeout);          }
148            if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
149          } else {          var f = this.frameloadtimeout.bind(this);
150                  var ifr = document.createElement("IFRAME");          this.frameloadtimer = setTimeout(f, 5000);
151                  ifr.style.width = "10px";  }
152                  ifr.style.height = "10px";  
153                  ifr.style.border = "none";  Meteor.prototype.stop = function() {
154                  if (document.all) {          if (typeof(this.transferDoc)=="object") {
155                          ifr.style.display = "none";                  this.transferDoc = false;
156                  } else {          }
157                          ifr.style.position = "absolute";          if (document.getElementById("meteorframe_"+this.instID)) {
158                          ifr.style.marginTop = "-10px";                  document.getElementById("meteorframe_"+this.instID).src="about:blank";
159                          ifr.style.zIndex = "-20";                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));
160                  }          }
161                  ifr.id = "meteorframe_"+this.instID;          if (!isNaN(this.pingtimer)) clearTimeout(this.pingtimer);
162                  document.body.appendChild(ifr);          if (!isNaN(this.updatepollfreqtimer)) clearTimeout(this.updatepollfreqtimer);
163                  ifr.src = "http://"+this.subdomain+"."+location.hostname+"/poll.html?nocache="+t;          if (!isNaN(this.frameloadtimer)) clearTimeout(this.frameloadtimer);
164                  this.recvtimes[0] = t;          this.setstatus(0);
165                  if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);  }
166                  this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);  
167          }  Meteor.prototype.pollmode = function() {
168          this.lastrequest = t;          if (this.debugmode) console.log("Ping timeout");
169  }          this.stop();
170            this.mode="poll";
171  Meteor.prototype.pollmode = function() {          this.start();
172          this.mode="poll";          this.callback_changemode("poll");
173          this.start();          this.lastpingtime = false;
174          this.callback_changemode("poll");  }
175  }  
176    Meteor.prototype.process = function(id, data, timestamp) {
177  Meteor.prototype.process = function(id, data) {          if (id > this.lastmsgreceived) {
178          if (id > this.lastmsgreceived) {                  this.callback_process(data, timestamp);
179                  this.callback_process(data);                  if (id != -1) this.lastmsgreceived = id;
180                  if (id != -1) this.lastmsgreceived = id;                  if (this.mode=="poll") {
181                  if (this.mode=="poll") {                          var now = new Date();
182                          var now = new Date();                          var t = now.getTime();
183                          var t = now.getTime();                          this.recvtimes[this.recvtimes.length] = t;
184                          this.recvtimes[this.recvtimes.length] = t;                          while (this.recvtimes.length > 5) this.recvtimes.shift();
185                          while (this.recvtimes.length > 5) this.recvtimes.shift();                  }
186                  }          } else if (id == -1) {
187          } else if (id == -1) {                  this.ping();
188                  this.ping();          }
189          }          this.setstatus(5);
190  }  }
191    
192  Meteor.prototype.ping = function() {  Meteor.prototype.ping = function() {
193          if (this.mode=="stream" && this.pingtimer) {          if (this.mode=="stream" && this.pingtimer) {
194                  clearTimeout(this.pingtimer);                  clearTimeout(this.pingtimer);
195                  var f = this.pollmode.bind(this);                  var f = this.pollmode.bind(this);
196                  this.pingtimer = setTimeout(f, this.pingtimeout);                  this.pingtimer = setTimeout(f, this.pingtimeout);
197                  var now = new Date();                  var now = new Date();
198                  this.lastpingtime = now.getTime();                  this.lastpingtime = now.getTime();
199          }          }
200  }          this.setstatus(5);
201    }
202  Meteor.prototype.reset = function() {  
203          var now = new Date();  Meteor.prototype.reset = function() {
204          var t = now.getTime();          if (this.debugmode) console.log("Stream reset");
205          var x = this.pollfreq - (t-this.lastrequest);          var now = new Date();
206          if (x < 10) x = 10;          var t = now.getTime();
207          this.ping();          var x = this.pollfreq - (t-this.lastrequest);
208          this.callback_reset();          if (x < 10) x = 10;
209          setTimeout(this.start.bind(this), x);          this.ping();
210  }          this.callback_reset();
211            setTimeout(this.start.bind(this), x);
212  Meteor.prototype.eof = function() {  }
213          this.callback_eof();  
214  }  Meteor.prototype.eof = function() {
215            this.callback_eof();
216  Meteor.prototype.get = function(varname) {  }
217          eval("var a = this."+varname+";");  
218          if (typeof(a) == "undefined") throw "Cannot get value of "+varname;  Meteor.prototype.get = function(varname) {
219          return a;          eval("var a = this."+varname+";");
220  }          if (typeof(a) == "undefined") throw "Cannot get value of "+varname;
221            return a;
222  Meteor.prototype.increasepolldelay = function() {  }
223          this.pollfreq *= 2;  
224  }  Meteor.prototype.increasepolldelay = function() {
225            this.pollfreq *= 2;
226  Meteor.prototype.updatepollfreq = function() {  }
227          if (this.smartpoll) {  
228                  var now = new Date();  Meteor.prototype.updatepollfreq = function() {
229                  var t = now.getTime();          if (this.smartpoll) {
230                  var avg = 0;                  var now = new Date();
231                  for (var i=1; i<this.recvtimes.length; i++) {                  var t = now.getTime();
232                          var x = (this.recvtimes[i]-this.recvtimes[i-1]);                  var avg = 0;
233                          avg += (x>60000)? 60000 : x;                  for (var i=1; i<this.recvtimes.length; i++) {
234                  }                          var x = (this.recvtimes[i]-this.recvtimes[i-1]);
235                  x = (t-this.recvtimes[this.recvtimes.length-1]);                          avg += (x>60000)? 60000 : x;
236                  avg += (x>180000)? 180000 : x;                  }
237                  avg /= this.recvtimes.length;                  x = (t-this.recvtimes[this.recvtimes.length-1]);
238                  if ((avg/3) < this.pollfreq && (avg/3) >= this.minpollfreq) this.pollfreq = Math.ceil(this.pollfreq*0.9);                  avg += (x>180000)? 180000 : x;
239                  if ((avg/3) > this.pollfreq) this.pollfreq = Math.floor(this.pollfreq*1.05);                  avg /= this.recvtimes.length;
240          }                  if ((avg/3) < this.pollfreq && (avg/3) >= this.minpollfreq) this.pollfreq = Math.ceil(this.pollfreq*0.9);
241  }                  if ((avg/3) > this.pollfreq) this.pollfreq = Math.floor(this.pollfreq*1.05);
242            }
243  Meteor.prototype.registerEventCallback = function(evt, funcRef) {  }
244          if (evt=="process") {  
245                  this.callback_process = (this.callback_process).andThen(funcRef);  Meteor.prototype.registerEventCallback = function(evt, funcRef) {
246          } else if (evt=="reset") {          if (evt=="process") {
247                  this.callback_reset = (this.callback_reset).andThen(funcRef);                  this.callback_process = (this.callback_process).andThen(funcRef);
248          } else if (evt=="eof") {          } else if (evt=="reset") {
249                  this.callback_eof = (this.callback_eof).andThen(funcRef);                  this.callback_reset = (this.callback_reset).andThen(funcRef);
250          } else if (evt=="changemode") {          } else if (evt=="eof") {
251                  this.callback_changemode = (this.callback_changemode).andThen(funcRef);                  this.callback_eof = (this.callback_eof).andThen(funcRef);
252          }          } else if (evt=="changemode") {
253  }                  this.callback_changemode = (this.callback_changemode).andThen(funcRef);
254            } else if (evt=="changestatus") {
255                    this.callback_statuschanged = (this.callback_statuschanged).andThen(funcRef);
256  Meteor.createCookie = function(name,value,days) {          }
257          if (days) {  }
258                  var date = new Date();  
259                  date.setTime(date.getTime()+(days*24*60*60*1000));  Meteor.prototype.frameloadtimeout = function() {
260                  var expires = "; expires="+date.toGMTString();          if (this.debugmode) console.log("Frame load timeout");
261          }          if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
262          else var expires = "";          this.setstatus(3);
263          document.cookie = name+"="+value+expires+"; path=/";          setTimeout(this.start.bind(this), 5000);
264  }  }
265    Meteor.prototype.setstatus = function(newstatus) {
266  Meteor.readCookie = function(name) {          if (this.status != newstatus) {
267          var nameEQ = name + "=";                  this.status = newstatus;
268          var ca = document.cookie.split(';');                  this.callback_statuschanged(newstatus);
269          for(var i=0;i < ca.length;i++) {          }
270                  var c = ca[i];  }
271                  while (c.charAt(0)==' ') c = c.substring(1,c.length);  
272                  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);  Meteor.createCookie = function(name,value,days) {
273          }          if (days) {
274          return null;                  var date = new Date();
275  }                  date.setTime(date.getTime()+(days*24*60*60*1000));
276                    var expires = "; expires="+date.toGMTString();
277  Meteor.eraseCookie = function(name) {          }
278          createCookie(name,"",-1);          else var expires = "";
279            document.cookie = name+"="+value+expires+"; path=/";
280    }
281    
282    Meteor.readCookie = function(name) {
283            var nameEQ = name + "=";
284            var ca = document.cookie.split(';');
285            for(var i=0;i < ca.length;i++) {
286                    var c = ca[i];
287                    while (c.charAt(0)==' ') c = c.substring(1,c.length);
288                    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
289            }
290            return null;
291    }
292    
293    Meteor.eraseCookie = function(name) {
294            createCookie(name,"",-1);
295  }  }

Legend:
Removed from v.3  
changed lines
  Added in v.14

  ViewVC Help
Powered by ViewVC 1.1.26