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

  ViewVC Help
Powered by ViewVC 1.1.26