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

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

  ViewVC Help
Powered by ViewVC 1.1.26