/[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 3 by andrew.betts, Mon Nov 20 17:59:30 2006 UTC revision 8 by andrew.betts, Thu Nov 23 16:35:37 2006 UTC
# Line 1  Line 1 
1  // Set domain at highest level  // Set domain at highest level
2  var domainparts = document.domain.split(".");  var domainparts = document.domain.split(".");
3  document.domain = domainparts[domainparts.length-2]+"."+domainparts[domainparts.length-1];  document.domain = domainparts[domainparts.length-2]+"."+domainparts[domainparts.length-1];
4    
5  Function.prototype.bind = function(obj) {  Function.prototype.bind = function(obj) {
6          var method = this,          var method = this,
7          temp = function() {          temp = function() {
8                  return method.apply(obj, arguments);                  return method.apply(obj, arguments);
9          };          };
10          return temp;          return temp;
11  }  }
12  Function.prototype.andThen=function(g) {  Function.prototype.andThen=function(g) {
13          var f=this;          var f=this;
14          var a=this.arguments          var a=this.arguments
15          return function(args) {          return function(args) {
16                  f(a);g(args);                  f(a);g(args);
17          }          }
18  };  };
19    function addUnLoadEvent(func) {
20  function Meteor(instID) {    var oldonunload = window.onunload;
21      if (typeof window.onunload != 'function') {
22          this.lastmsgreceived = -1;      window.onunload = func;
23          this.transferDoc = false;    } else {
24          this.pingtimer = false;      window.onunload = function() {
25          this.updatepollfreqtimer = false;        if (oldonunload) {
26          this.lastrequest = 0;          oldonunload();
27          this.recvtimes = new Array();        }
28          this.MHostId = false;        func();
29          this.callback_process = function() {};      }
30          this.callback_reset = function() {};    }
31          this.callback_eof = function() {};  }
32          this.callback_changemode = function() {};  addUnLoadEvent(meteordestroy);
33          this.persist = true;  function meteordestroy() {
34            var x = Meteor.instances.length;
35          // Documented public properties          for(var i=0; i<x; i++) {
36          this.channel = false;                  if (typeof(Meteor.instances[i].transferDoc)=="object") {
37          this.subdomain = "data";                          Meteor.instances[i].transferDoc.open();
38          this.dynamicpageaddress = "push";                          Meteor.instances[i].transferDoc.close();
39          this.backtrack = 0;                          delete Meteor.instances[i].transferDoc;
40          this.smartpoll = true;                  }
41          this.pollfreq = 2000;                  if (document.getElementById("meteorframe_"+Meteor.instances[i].instID)) {
42          this.minpollfreq = 2000;                          document.body.removeChild(document.getElementById("meteorframe_"+Meteor.instances[i].instID));
43          this.mode = "stream";                  }
44          this.polltimeout=30000;                  delete Meteor.instances[i];
45          this.maxmessages=0;          }
46          this.pingtimeout = 10000;  }
47    
48          // Set or retrieve host id.  Cookie takes this form:  function Meteor(instID) {
49          // MeteorID=123:6356353/124:098320454;  
50          var MeteIds = Meteor.readCookie("MeteorID");          this.lastmsgreceived = -1;
51          var regex1 = new RegExp("^([0-9\:\/M]+\/)*"+instID+"\:([^\/]+)(\/[0-9\:\/M]+)*$");          this.transferDoc = false;
52          var regex2 = new RegExp("^([0-9\:\/M]+\/)*M\:([^\/]+)(\/[0-9\:\/M]+)*$");          this.pingtimer = false;
53          if (typeof(instID) == "Number" && regex1.exec(MeteIds)) {          this.updatepollfreqtimer = false;
54                  this.MHostId = ma[2];          this.lastrequest = 0;
55          } else if (typeof(instID) == "Number") {          this.recvtimes = new Array();
56                  this.MHostId = Math.floor(Math.random()*1000000);          this.MHostId = false;
57                  var newcookie = (MeteIds)?MeteIds+"/":"";          this.callback_process = function() {};
58                  newcookie += instID+":"+this.MHostId;          this.callback_reset = function() {};
59                  Meteor.createCookie("MeteorID", newcookie);          this.callback_eof = function() {};
60          } else if (ma = regex2.exec(MeteIds)) {          this.callback_changemode = function() {};
61                  this.MHostId = ma[2];          this.callback_statuschanged = function() {};
62          } else {          this.persist = true;
63                  this.MHostId = Math.floor(Math.random()*1000000);          this.frameloadtimer = false;
64                  var newcookie = (MeteIds)?MeteIds+"/":"";          this.frameurl = false;
65                  newcookie += "M:"+this.MHostId;  
66                  Meteor.createCookie("MeteorID", newcookie);          // Documented public properties
67          }          this.channel = false;
68          this.instID = (typeof(instID) != "undefined") ? instID : 0;          this.subdomain = "data";
69  }          this.dynamicpageaddress = "push";
70            this.backtrack = 0;
71  Meteor.instances = new Array();          this.smartpoll = true;
72  Meteor.servertimeoffset = 0;          this.pollfreq = 2000;
73            this.minpollfreq = 2000;
74  Meteor.create = function(instID) {          this.mode = "stream";
75          if (!instID) instID = 0;          this.polltimeout=30000;
76          Meteor.instances[instID] = new Meteor(instID);          this.maxmessages=0;
77          return Meteor.instances[instID];          this.pingtimeout = 10000;
78  }          this.status = 0;
79    
80  Meteor.register = function(ifr) {          /* Statuses:    0 = Uninitialised,
81          instid = new String(ifr.window.frameElement.id);                                          1 = Loading stream,
82          instid = instid.replace("meteorframe_", "");                                          2 = Loading controller frame,
83          ifr.p = this.instances[instid].process.bind(this.instances[instid]);                                          3 = Controller frame timeout, retrying every 5 seconds
84          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);                                          4 = Controller frame loaded and ready
85          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);                                          5 = Receiving data
86          ifr.get = this.instances[instid].get.bind(this.instances[instid]);          */
87          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);  
88  }          // Set or retrieve host id.  Cookie takes this form:
89            // MeteorID=123:6356353/124:098320454;
90  Meteor.setServerTime = function(timestamp) {          var MeteIds = Meteor.readCookie("MeteorID");
91          var now = new Date();          var regex1 = new RegExp("^([0-9\:\/M]+\/)*"+instID+"\:([^\/]+)(\/[0-9\:\/M]+)*$");
92          var clienttime = (now.getTime() / 1000);          var regex2 = new RegExp("^([0-9\:\/M]+\/)*M\:([^\/]+)(\/[0-9\:\/M]+)*$");
93          Meteor.servertimeoffset = timestamp - clienttime;          if (typeof(instID) == "Number" && regex1.exec(MeteIds)) {
94  }                  this.MHostId = ma[2];
95            } else if (typeof(instID) == "Number") {
96  Meteor.prototype.start = function() {                  this.MHostId = Math.floor(Math.random()*1000000);
97          this.persist = (this.maxmessages)?1:0;                  var newcookie = (MeteIds)?MeteIds+"/":"";
98          this.smartpoll = (this.smartpoll)?1:0;                  newcookie += instID+":"+this.MHostId;
99          this.mode = (this.mode=="stream")?"stream":"poll";                  Meteor.createCookie("MeteorID", newcookie);
100          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";          } else if (ma = regex2.exec(MeteIds)) {
101          var now = new Date();                  this.MHostId = ma[2];
102          var t = now.getTime();          } else {
103          if (typeof(this.transferDoc)=="object") {                  this.MHostId = Math.floor(Math.random()*1000000);
104                  this.transferDoc.open();                  var newcookie = (MeteIds)?MeteIds+"/":"";
105                  this.transferDoc.close();                  newcookie += "M:"+this.MHostId;
106                  delete this.transferDoc;                  Meteor.createCookie("MeteorID", newcookie);
107          }          }
108          if (document.getElementById("meteorframe_"+this.instID)) {          this.instID = (typeof(instID) != "undefined") ? instID : 0;
109                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));  }
110          }  
111          if (this.mode=="stream") {  Meteor.instances = new Array();
112                  if (document.all) {  Meteor.servertimeoffset = 0;
113                          this.transferDoc = new ActiveXObject("htmlfile");  
114                          this.transferDoc.open();  Meteor.create = function(instID) {
115                          this.transferDoc.write("<html>");          if (!instID) instID = 0;
116                          this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");          Meteor.instances[instID] = new Meteor(instID);
117                          this.transferDoc.write("</html>");          return Meteor.instances[instID];
118                          var selfref = this;  }
119                          this.transferDoc.parentWindow.Meteor = Meteor;  
120                          this.transferDoc.close();  Meteor.register = function(ifr) {
121                          var ifrDiv = this.transferDoc.createElement("div");          instid = new String(ifr.window.frameElement.id);
122                          this.transferDoc.appendChild(ifrDiv);          instid = instid.replace("meteorframe_", "");
123                          var url = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId;          ifr.p = this.instances[instid].process.bind(this.instances[instid]);
124                          if (this.lastmsgreceived >= 0) {          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);
125                                  url += "&restartfrom="+this.lastmsgreceived;          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);
126                          } else if (this.backtrack > 0) {          ifr.get = this.instances[instid].get.bind(this.instances[instid]);
127                                  url += "&backtrack="+this.backtrack;          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);
128                          } else if (this.backtrack < 0 || isNaN(this.backtrack)) {          clearTimeout(this.instances[instid].frameloadtimer);
129                                  url += "&restartfrom=";          this.instances[instid].setstatus(4);
130                          }  }
131                          ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"&nocache="+t+"\" style=\"display: none;\"></iframe>";  
132                  } else {  Meteor.setServerTime = function(timestamp) {
133                          var ifr = document.createElement("IFRAME");          var now = new Date();
134                          ifr.style.width = "10px";          var clienttime = (now.getTime() / 1000);
135                          ifr.style.height = "10px";          Meteor.servertimeoffset = timestamp - clienttime;
136                          ifr.style.border = "none";  }
137                          ifr.style.position = "absolute";  
138                          ifr.style.top = "-10px";  Meteor.prototype.start = function() {
139                          ifr.style.marginTop = "-10px";          this.persist = (this.maxmessages)?1:0;
140                          ifr.style.zIndex = "-20";          this.smartpoll = (this.smartpoll)?1:0;
141                          ifr.id = "meteorframe_"+this.instID;          this.mode = (this.mode=="stream")?"stream":"poll";
142                          document.body.appendChild(ifr);          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";
143                          ifr.src = "http://"+this.subdomain+"."+location.hostname+"/stream.html?nocache="+t;          var now = new Date();
144                  }          var t = now.getTime();
145                  var f = this.pollmode.bind(this);          if (typeof(this.transferDoc)=="object") {
146                  clearTimeout(this.pingtimer);                  this.transferDoc.open();
147                  this.pingtimer = setTimeout(f, this.pingtimeout);                  this.transferDoc.close();
148                    delete this.transferDoc;
149          } else {          }
150                  var ifr = document.createElement("IFRAME");          if (document.getElementById("meteorframe_"+this.instID)) {
151                  ifr.style.width = "10px";                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));
152                  ifr.style.height = "10px";          }
153                  ifr.style.border = "none";          if (this.mode=="stream") {
154                  if (document.all) {                  if (document.all) {
155                          ifr.style.display = "none";                          this.setstatus(1);
156                  } else {                          this.transferDoc = new ActiveXObject("htmlfile");
157                          ifr.style.position = "absolute";                          this.transferDoc.open();
158                          ifr.style.marginTop = "-10px";                          this.transferDoc.write("<html>");
159                          ifr.style.zIndex = "-20";                          this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
160                  }                          this.transferDoc.write("</html>");
161                  ifr.id = "meteorframe_"+this.instID;                          var selfref = this;
162                  document.body.appendChild(ifr);                          this.transferDoc.parentWindow.Meteor = Meteor;
163                  ifr.src = "http://"+this.subdomain+"."+location.hostname+"/poll.html?nocache="+t;                          this.transferDoc.close();
164                  this.recvtimes[0] = t;                          var ifrDiv = this.transferDoc.createElement("div");
165                  if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);                          this.transferDoc.appendChild(ifrDiv);
166                  this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);                          var url = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId;
167          }                          if (this.lastmsgreceived >= 0) {
168          this.lastrequest = t;                                  url += "&restartfrom="+this.lastmsgreceived;
169  }                          } else if (this.backtrack > 0) {
170                                    url += "&backtrack="+this.backtrack;
171  Meteor.prototype.pollmode = function() {                          } else if (this.backtrack < 0 || isNaN(this.backtrack)) {
172          this.mode="poll";                                  url += "&restartfrom=";
173          this.start();                          }
174          this.callback_changemode("poll");                          ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"&nocache="+t+"\" style=\"display: none;\"></iframe>";
175  }                  } else {
176                            var ifr = document.createElement("IFRAME");
177  Meteor.prototype.process = function(id, data) {                          ifr.style.width = "10px";
178          if (id > this.lastmsgreceived) {                          ifr.style.height = "10px";
179                  this.callback_process(data);                          ifr.style.border = "none";
180                  if (id != -1) this.lastmsgreceived = id;                          ifr.style.position = "absolute";
181                  if (this.mode=="poll") {                          ifr.style.top = "-10px";
182                          var now = new Date();                          ifr.style.marginTop = "-10px";
183                          var t = now.getTime();                          ifr.style.zIndex = "-20";
184                          this.recvtimes[this.recvtimes.length] = t;                          ifr.id = "meteorframe_"+this.instID;
185                          while (this.recvtimes.length > 5) this.recvtimes.shift();                          document.body.appendChild(ifr);
186                  }                          this.frameurl = "http://"+this.subdomain+"."+location.hostname+"/stream.html";
187          } else if (id == -1) {                          this.frameload();
188                  this.ping();                  }
189          }                  var f = this.pollmode.bind(this);
190  }                  clearTimeout(this.pingtimer);
191                    this.pingtimer = setTimeout(f, this.pingtimeout);
192  Meteor.prototype.ping = function() {  
193          if (this.mode=="stream" && this.pingtimer) {          } else {
194                  clearTimeout(this.pingtimer);                  var ifr = document.createElement("IFRAME");
195                  var f = this.pollmode.bind(this);                  ifr.style.width = "10px";
196                  this.pingtimer = setTimeout(f, this.pingtimeout);                  ifr.style.height = "10px";
197                  var now = new Date();                  ifr.style.border = "none";
198                  this.lastpingtime = now.getTime();                  if (document.all) {
199          }                          ifr.style.display = "none";
200  }                  } else {
201                            ifr.style.position = "absolute";
202  Meteor.prototype.reset = function() {                          ifr.style.marginTop = "-10px";
203          var now = new Date();                          ifr.style.zIndex = "-20";
204          var t = now.getTime();                  }
205          var x = this.pollfreq - (t-this.lastrequest);                  ifr.id = "meteorframe_"+this.instID;
206          if (x < 10) x = 10;                  document.body.appendChild(ifr);
207          this.ping();                  this.frameurl = "http://"+this.subdomain+"."+location.hostname+"/poll.html";
208          this.callback_reset();                  this.frameload();
209          setTimeout(this.start.bind(this), x);                  this.recvtimes[0] = t;
210  }                  if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);
211                    this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);
212  Meteor.prototype.eof = function() {          }
213          this.callback_eof();          this.lastrequest = t;
214  }  }
215    
216  Meteor.prototype.get = function(varname) {  Meteor.prototype.pollmode = function() {
217          eval("var a = this."+varname+";");          this.mode="poll";
218          if (typeof(a) == "undefined") throw "Cannot get value of "+varname;          this.start();
219          return a;          this.callback_changemode("poll");
220  }          this.lastpingtime = false;
221    }
222  Meteor.prototype.increasepolldelay = function() {  
223          this.pollfreq *= 2;  Meteor.prototype.process = function(id, data) {
224  }          if (id > this.lastmsgreceived) {
225                    this.callback_process(data);
226  Meteor.prototype.updatepollfreq = function() {                  if (id != -1) this.lastmsgreceived = id;
227          if (this.smartpoll) {                  if (this.mode=="poll") {
228                  var now = new Date();                          var now = new Date();
229                  var t = now.getTime();                          var t = now.getTime();
230                  var avg = 0;                          this.recvtimes[this.recvtimes.length] = t;
231                  for (var i=1; i<this.recvtimes.length; i++) {                          while (this.recvtimes.length > 5) this.recvtimes.shift();
232                          var x = (this.recvtimes[i]-this.recvtimes[i-1]);                  }
233                          avg += (x>60000)? 60000 : x;          } else if (id == -1) {
234                  }                  this.ping();
235                  x = (t-this.recvtimes[this.recvtimes.length-1]);          }
236                  avg += (x>180000)? 180000 : x;          this.setstatus(5);
237                  avg /= this.recvtimes.length;  }
238                  if ((avg/3) < this.pollfreq && (avg/3) >= this.minpollfreq) this.pollfreq = Math.ceil(this.pollfreq*0.9);  
239                  if ((avg/3) > this.pollfreq) this.pollfreq = Math.floor(this.pollfreq*1.05);  Meteor.prototype.ping = function() {
240          }          if (this.mode=="stream" && this.pingtimer) {
241  }                  clearTimeout(this.pingtimer);
242                    var f = this.pollmode.bind(this);
243  Meteor.prototype.registerEventCallback = function(evt, funcRef) {                  this.pingtimer = setTimeout(f, this.pingtimeout);
244          if (evt=="process") {                  var now = new Date();
245                  this.callback_process = (this.callback_process).andThen(funcRef);                  this.lastpingtime = now.getTime();
246          } else if (evt=="reset") {          }
247                  this.callback_reset = (this.callback_reset).andThen(funcRef);          this.setstatus(5);
248          } else if (evt=="eof") {  }
249                  this.callback_eof = (this.callback_eof).andThen(funcRef);  
250          } else if (evt=="changemode") {  Meteor.prototype.reset = function() {
251                  this.callback_changemode = (this.callback_changemode).andThen(funcRef);          var now = new Date();
252          }          var t = now.getTime();
253  }          var x = this.pollfreq - (t-this.lastrequest);
254            if (x < 10) x = 10;
255            this.ping();
256  Meteor.createCookie = function(name,value,days) {          this.callback_reset();
257          if (days) {          setTimeout(this.start.bind(this), x);
258                  var date = new Date();  }
259                  date.setTime(date.getTime()+(days*24*60*60*1000));  
260                  var expires = "; expires="+date.toGMTString();  Meteor.prototype.eof = function() {
261          }          this.callback_eof();
262          else var expires = "";  }
263          document.cookie = name+"="+value+expires+"; path=/";  
264  }  Meteor.prototype.get = function(varname) {
265            eval("var a = this."+varname+";");
266  Meteor.readCookie = function(name) {          if (typeof(a) == "undefined") throw "Cannot get value of "+varname;
267          var nameEQ = name + "=";          return a;
268          var ca = document.cookie.split(';');  }
269          for(var i=0;i < ca.length;i++) {  
270                  var c = ca[i];  Meteor.prototype.increasepolldelay = function() {
271                  while (c.charAt(0)==' ') c = c.substring(1,c.length);          this.pollfreq *= 2;
272                  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);  }
273          }  
274          return null;  Meteor.prototype.updatepollfreq = function() {
275  }          if (this.smartpoll) {
276                    var now = new Date();
277  Meteor.eraseCookie = function(name) {                  var t = now.getTime();
278          createCookie(name,"",-1);                  var avg = 0;
279                    for (var i=1; i<this.recvtimes.length; i++) {
280                            var x = (this.recvtimes[i]-this.recvtimes[i-1]);
281                            avg += (x>60000)? 60000 : x;
282                    }
283                    x = (t-this.recvtimes[this.recvtimes.length-1]);
284                    avg += (x>180000)? 180000 : x;
285                    avg /= this.recvtimes.length;
286                    if ((avg/3) < this.pollfreq && (avg/3) >= this.minpollfreq) this.pollfreq = Math.ceil(this.pollfreq*0.9);
287                    if ((avg/3) > this.pollfreq) this.pollfreq = Math.floor(this.pollfreq*1.05);
288            }
289    }
290    
291    Meteor.prototype.registerEventCallback = function(evt, funcRef) {
292            if (evt=="process") {
293                    this.callback_process = (this.callback_process).andThen(funcRef);
294            } else if (evt=="reset") {
295                    this.callback_reset = (this.callback_reset).andThen(funcRef);
296            } else if (evt=="eof") {
297                    this.callback_eof = (this.callback_eof).andThen(funcRef);
298            } else if (evt=="changemode") {
299                    this.callback_changemode = (this.callback_changemode).andThen(funcRef);
300            } else if (evt=="changestatus") {
301                    this.callback_statuschanged = (this.callback_statuschanged).andThen(funcRef);
302            }
303    }
304    
305    Meteor.prototype.frameload = function() {
306            this.setstatus(2);
307            if (document.getElementById("meteorframe_"+this.instID)) {
308                    var f = this.frameloadtimeout.bind(this);
309                    this.frameloadtimer = setTimeout(f, 5000);
310                    document.getElementById("meteorframe_"+this.instID).src = "about:blank";
311                    setTimeout(this.doloadurl.bind(this), 100);
312            }
313    }
314    Meteor.prototype.doloadurl = function() {
315            var now = new Date();
316            var t = now.getTime();
317            document.getElementById("meteorframe_"+this.instID).src = this.frameurl+"?nocache="+t;
318    }
319    Meteor.prototype.frameloadtimeout = function() {
320            if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
321            this.setstatus(3);
322            this.frameload();
323    }
324    Meteor.prototype.setstatus = function(newstatus) {
325            if (this.status != newstatus) {
326                    this.status = newstatus;
327                    this.callback_statuschanged(newstatus);
328            }
329    }
330    
331    Meteor.createCookie = function(name,value,days) {
332            if (days) {
333                    var date = new Date();
334                    date.setTime(date.getTime()+(days*24*60*60*1000));
335                    var expires = "; expires="+date.toGMTString();
336            }
337            else var expires = "";
338            document.cookie = name+"="+value+expires+"; path=/";
339    }
340    
341    Meteor.readCookie = function(name) {
342            var nameEQ = name + "=";
343            var ca = document.cookie.split(';');
344            for(var i=0;i < ca.length;i++) {
345                    var c = ca[i];
346                    while (c.charAt(0)==' ') c = c.substring(1,c.length);
347                    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
348            }
349            return null;
350    }
351    
352    Meteor.eraseCookie = function(name) {
353            createCookie(name,"",-1);
354  }  }

Legend:
Removed from v.3  
changed lines
  Added in v.8

  ViewVC Help
Powered by ViewVC 1.1.26