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

Legend:
Removed from v.6  
changed lines
  Added in v.30

  ViewVC Help
Powered by ViewVC 1.1.26