/[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 39 by andrew.betts, Sat Feb 2 16:49:20 2008 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: 3000,
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) {                          clearTimeout(Meteor.pingtimer);
95          instid = new String(ifr.window.frameElement.id);                          clearTimeout(Meteor.updatepollfreqtimer);
96          instid = instid.replace("meteorframe_", "");                          clearTimeout(Meteor.frameloadtimer);
97          ifr.p = this.instances[instid].process.bind(this.instances[instid]);                          if (typeof CollectGarbage == 'function') CollectGarbage();
98          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);                          Meteor.setstatus(0);
99          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);                  }
100          ifr.get = this.instances[instid].get.bind(this.instances[instid]);          },
101          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);          
102          clearTimeout(this.instances[instid].frameloadtimer);          selectStreamTransport: function() {
103          this.instances[instid].setstatus(4);                  try {
104  }                          var test = ActiveXObject;
105                            return "iframe";
106  Meteor.setServerTime = function(timestamp) {                  } catch (e) {}
107          var now = new Date();                  if ((typeof window.addEventStream) == "function") return "iframe";
108          var clienttime = (now.getTime() / 1000);                  return "xhrinteractive";
109          Meteor.servertimeoffset = timestamp - clienttime;          },
110  }  
111            getSubsUrl: function() {
112  Meteor.prototype.start = function() {                  var surl = "http://" + Meteor.host + ((Meteor.port==80)?"":":"+Meteor.port) + "/push/" + Meteor.hostid + "/" + Meteor.mode;
113          this.persist = (this.maxmessages)?1:0;                  for (var c in Meteor.channels) {
114          this.smartpoll = (this.smartpoll)?1:0;                          surl += "/"+c;
115          this.mode = (this.mode=="stream")?"stream":"poll";                          if (Meteor.channels[c].lastmsgreceived > 0) {
116          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";                                  surl += ".r"+(Meteor.channels[c].lastmsgreceived+1);
117          var now = new Date();                          } else if (Meteor.channels[c].backtrack > 0) {
118          var t = now.getTime();                                  surl += ".b"+Meteor.channels[c].backtrack;
119          if (typeof(this.transferDoc)=="object") {                          } else if (Meteor.channels[c].backtrack < 0 || isNaN(Meteor.channels[c].backtrack)) {
120                  this.transferDoc.open();                                  surl += ".h";
121                  this.transferDoc.close();                          }
122                  delete this.transferDoc;                  }
123          }                  return surl;
124          if (document.getElementById("meteorframe_"+this.instID)) {          },
125                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));  
126          }          loadFrame: function(url) {
127          if (this.mode=="stream") {                  try {
128                  if (document.all) {                          if (!Meteor.frameref) {
129                          this.setstatus(1);                                  var transferDoc = new ActiveXObject("htmlfile");
130                          this.transferDoc = new ActiveXObject("htmlfile");                                  Meteor.frameref = transferDoc;
131                          this.transferDoc.open();                          }
132                          this.transferDoc.write("<html>");                          Meteor.frameref.open();
133                          this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");                          Meteor.frameref.write("<html><script>");
134                          this.transferDoc.write("</html>");                          Meteor.frameref.write("document.domain=\""+(document.domain)+"\";");
135                          var selfref = this;                          Meteor.frameref.write("</"+"script></html>");
136                          this.transferDoc.parentWindow.Meteor = Meteor;                          Meteor.frameref.parentWindow.Meteor = Meteor;
137                          this.transferDoc.close();                          Meteor.frameref.close();
138                          var ifrDiv = this.transferDoc.createElement("div");                          var ifrDiv = Meteor.frameref.createElement("div");
139                          this.transferDoc.appendChild(ifrDiv);                          Meteor.frameref.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) {                  } catch (e) {
142                                  url += "&restartfrom="+this.lastmsgreceived;                          if (!Meteor.frameref) {
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";                                  document.body.appendChild(ifr);
153                          ifr.style.border = "none";                                  Meteor.frameref = ifr;
154                          ifr.style.position = "absolute";                          }
155                          ifr.style.top = "-10px";                          Meteor.frameref.setAttribute("src", url);
156                          ifr.style.marginTop = "-10px";                  }
157                          ifr.style.zIndex = "-20";                  Meteor.log("Loading URL '"+url+"' into frame...");
158                          ifr.id = "meteorframe_"+this.instID;                  Meteor.frameloadtimer = setTimeout(Meteor.frameloadtimeout, 5000);
159                          document.body.appendChild(ifr);          },
160                          this.frameurl = "http://"+this.subdomain+"."+location.hostname+"/stream.html";  
161                          this.frameload();          pollmode: function() {
162                  }                  Meteor.log("Ping timeout");
163                  var f = this.pollmode.bind(this);                  Meteor.mode="simplepoll";
164                  clearTimeout(this.pingtimer);                  clearTimeout(Meteor.pingtimer);
165                  this.pingtimer = setTimeout(f, this.pingtimeout);                  Meteor.connect();
166                    Meteor.callbacks["changemode"]("poll");
167          } else {                  Meteor.lastpingtime = false;
168                  var ifr = document.createElement("IFRAME");          },
169                  ifr.style.width = "10px";  
170                  ifr.style.height = "10px";          process: function(id, channel, data) {
171                  ifr.style.border = "none";                  if (id == -1) {
172                  if (document.all) {                          Meteor.log("Ping");
173                          ifr.style.display = "none";                          Meteor.ping();
174                  } else {                  } else if (typeof(Meteor.channels[channel]) != "undefined" && id > Meteor.channels[channel].lastmsgreceived) {
175                          ifr.style.position = "absolute";                          Meteor.log("Message "+id+" received on channel "+channel+" (last id on channel: "+Meteor.channels[channel].lastmsgreceived+")\n"+data);
176                          ifr.style.marginTop = "-10px";                          Meteor.callbacks["process"](data);
177                          ifr.style.zIndex = "-20";                          Meteor.channels[channel].lastmsgreceived = id;
178                  }                          if (Meteor.mode=="smartpoll") {
179                  ifr.id = "meteorframe_"+this.instID;                                  var now = new Date();
180                  document.body.appendChild(ifr);                                  Meteor.recvtimes[Meteor.recvtimes.length] = now.getTime();
181                  this.frameurl = "http://"+this.subdomain+"."+location.hostname+"/poll.html";                                  while (Meteor.recvtimes.length > 5) Meteor.recvtimes.shift();
182                  this.frameload();                          }
183                  this.recvtimes[0] = t;                  }
184                  if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);                  Meteor.setstatus(5);
185                  this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);          },
186          }  
187          this.lastrequest = t;          ping: function() {
188  }                  if (Meteor.pingtimer) {
189                            clearTimeout(Meteor.pingtimer);
190  Meteor.prototype.stop = function() {                          Meteor.pingtimer = setTimeout(Meteor.pollmode, Meteor.pingtimeout);
191          if (typeof(this.transferDoc)=="object") {                          var now = new Date();
192                  this.transferDoc.open();                          Meteor.lastpingtime = now.getTime();
193                  this.transferDoc.close();                  }
194                  delete this.transferDoc;                  Meteor.setstatus(5);
195          }          },
196          if (document.getElementById("meteorframe_"+this.instID)) {  
197                  document.getElementById("meteorframe_"+this.instID).src="about:blank";          reset: function() {
198                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));                  Meteor.log("Stream reset");
199          }                  Meteor.ping();
200          if (!isNaN(this.pingtimer)) clearTimeout(this.pingtimer);                  Meteor.callbacks["reset"]();
201          if (!isNaN(this.updatepollfreqtimer)) clearTimeout(this.updatepollfreqtimer);                  var now = new Date();
202          if (!isNaN(this.frameloadtimer)) clearTimeout(this.frameloadtimer);                  var t = now.getTime();
203          this.setstatus(0);                  var x = Meteor.pollfreq - (t-Meteor.lastrequest);
204  }                  if (x < 10) x = 10;
205                    setTimeout(Meteor.connect, x);
206  Meteor.prototype.pollmode = function() {          },
207          this.mode="poll";  
208          this.start();          eof: function() {
209          this.callback_changemode("poll");                  Meteor.callbacks["eof"]();
210          this.lastpingtime = false;          },
211  }  
212            updatepollfreq: function() {
213  Meteor.prototype.process = function(id, data) {                  var now = new Date();
214          if (id > this.lastmsgreceived) {                  var t = now.getTime();
215                  this.callback_process(data);                  var avg = 0;
216                  if (id != -1) this.lastmsgreceived = id;                  for (var i=1; i<Meteor.recvtimes.length; i++) {
217                  if (this.mode=="poll") {                          avg += (Meteor.recvtimes[i]-Meteor.recvtimes[i-1]);
218                          var now = new Date();                  }
219                          var t = now.getTime();                  avg += (t-Meteor.recvtimes[Meteor.recvtimes.length-1]);
220                          this.recvtimes[this.recvtimes.length] = t;                  avg /= Meteor.recvtimes.length;
221                          while (this.recvtimes.length > 5) this.recvtimes.shift();                  var target = avg/2;
222                  }                  if (target < Meteor.pollfreq && Meteor.pollfreq > Meteor.minpollfreq) Meteor.pollfreq = Math.ceil(Meteor.pollfreq*0.9);
223          } else if (id == -1) {                  if (target > Meteor.pollfreq && Meteor.pollfreq < Meteor.maxpollfreq) Meteor.pollfreq = Math.floor(Meteor.pollfreq*1.05);
224                  this.ping();          },
225          }  
226          this.setstatus(5);          registerEventCallback: function(evt, funcRef) {
227  }                  Function.prototype.andThen=function(g) {
228                            var f=this;
229  Meteor.prototype.ping = function() {                          var a=Meteor.arguments
230          if (this.mode=="stream" && this.pingtimer) {                          return function(args) {
231                  clearTimeout(this.pingtimer);                                  f(a);g(args);
232                  var f = this.pollmode.bind(this);                          }
233                  this.pingtimer = setTimeout(f, this.pingtimeout);                  };
234                  var now = new Date();                  if (typeof Meteor.callbacks[evt] == "function") {
235                  this.lastpingtime = now.getTime();                          Meteor.callbacks[evt] = (Meteor.callbacks[evt]).andThen(funcRef);
236          }                  } else {
237          this.setstatus(5);                          Meteor.callbacks[evt] = funcRef;
238  }                  }
239            },
240  Meteor.prototype.reset = function() {  
241          var now = new Date();          frameloadtimeout: function() {
242          var t = now.getTime();                  Meteor.log("Frame load timeout");
243          var x = this.pollfreq - (t-this.lastrequest);                  if (Meteor.frameloadtimer) clearTimeout(Meteor.frameloadtimer);
244          if (x < 10) x = 10;                  Meteor.setstatus(3);
245          this.ping();                  setTimeout(Meteor.connect, 5000);
246          this.callback_reset();          },
247          setTimeout(this.start.bind(this), x);  
248  }          extract_xss_domain: function(old_domain) {
249                    if (old_domain.match(/^(\d{1,3}\.){3}\d{1,3}$/)) return old_domain;
250  Meteor.prototype.eof = function() {                  domain_pieces = old_domain.split('.');
251          this.callback_eof();                  return domain_pieces.slice(-2, domain_pieces.length).join(".");
252  }          },
253    
254  Meteor.prototype.get = function(varname) {          setstatus: function(newstatus) {
255          eval("var a = this."+varname+";");                  // Statuses:    0 = Uninitialised,
256          if (typeof(a) == "undefined") throw "Cannot get value of "+varname;                  //                              1 = Loading stream,
257          return a;                  //                              2 = Loading controller frame,
258  }                  //                              3 = Controller frame timeout, retrying.
259                    //                              4 = Controller frame loaded and ready
260  Meteor.prototype.increasepolldelay = function() {                  //                              5 = Receiving data
261          this.pollfreq *= 2;  
262  }                  if (Meteor.status != newstatus) {
263                            Meteor.status = newstatus;
264  Meteor.prototype.updatepollfreq = function() {                          Meteor.callbacks["statuschanged"](newstatus);
265          if (this.smartpoll) {                  }
266                  var now = new Date();          },
267                  var t = now.getTime();  
268                  var avg = 0;          log: function(logstr) {
269                  for (var i=1; i<this.recvtimes.length; i++) {                  if (Meteor.debugmode) {
270                          var x = (this.recvtimes[i]-this.recvtimes[i-1]);                          if (window.console) {
271                          avg += (x>60000)? 60000 : x;                                  window.console.log(logstr);
272                  }                          } else if (document.getElementById("meteorlogoutput")) {
273                  x = (t-this.recvtimes[this.recvtimes.length-1]);                                  document.getElementById("meteorlogoutput").innerHTML += logstr+"<br/>";
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          }  
279  }  var oldonunload = window.onunload;
280    if (typeof window.onunload != 'function') {
281  Meteor.prototype.registerEventCallback = function(evt, funcRef) {          window.onunload = Meteor.disconnect;
282          if (evt=="process") {  } else {
283                  this.callback_process = (this.callback_process).andThen(funcRef);          window.onunload = function() {
284          } else if (evt=="reset") {                  if (oldonunload) oldonunload();
285                  this.callback_reset = (this.callback_reset).andThen(funcRef);                  Meteor.disconnect();
286          } 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);  
287  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26