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

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

  ViewVC Help
Powered by ViewVC 1.1.26