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

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

  ViewVC Help
Powered by ViewVC 1.1.26