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

Legend:
Removed from v.9  
changed lines
  Added in v.53

  ViewVC Help
Powered by ViewVC 1.1.26