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

Legend:
Removed from v.10  
changed lines
  Added in v.32

  ViewVC Help
Powered by ViewVC 1.1.26