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

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

  ViewVC Help
Powered by ViewVC 1.1.26