/[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 7 by andrew.betts, Thu Nov 23 15:53:25 2006 UTC revision 28 by andrew.betts, Thu Aug 2 09:29:07 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.callback_statuschanged = function() {};          this.callback_statuschanged = function() {};
33          this.persist = true;          this.persist = 1;
34          this.frameloadtimer = false;          this.frameloadtimer = false;
35          this.frameurl = false;          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;          this.status = 0;
53    
54          /* Statuses:    0 = Uninitialised,          /* Statuses:    0 = Uninitialised,
55                                          1 = Loading stream,                                          1 = Loading stream,
56                                          2 = Loading controller frame,                                          2 = Loading controller frame,
57                                          3 = Controller frame timeout, retrying every 5 seconds                                          3 = Controller frame timeout, retrying.
58                                          4 = Controller frame loaded and ready                                          4 = Controller frame loaded and ready
59                                          5 = Receiving data                                          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]);
# Line 99  Meteor.register = function(ifr) { Line 81  Meteor.register = function(ifr) {
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);          clearTimeout(this.instances[instid].frameloadtimer);
83          this.instances[instid].setstatus(4);          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    Meteor.prototype.joinChannel = function(channelname, backtrack) {
94            if (typeof(this.channels[channelname]) != "undefined") throw "Cannot join channel "+channelname+": already subscribed";
95            this.channels[channelname] = {backtrack:backtrack, lastmsgreceived:0};
96            if (this.debugmode) console.log("Joined channel "+channelname+", channel list follows");
97            if (this.debugmode) console.log(this.channels);
98            if (this.status != 0) this.start();
99            this.channelcount++;
100    }
101    
102    Meteor.prototype.leaveChannel = function(channelname) {
103            if (typeof(this.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed";
104            delete this.channels[channelname];
105            if (this.status != 0) this.start();
106            this.channelcount--;
107  }  }
108    
109  Meteor.prototype.start = function() {  Meteor.prototype.start = function() {
110          this.persist = (this.maxmessages)?1:0;          this.persist = (this.persist)?1:0;
111          this.smartpoll = (this.smartpoll)?1:0;          this.smartpoll = (this.smartpoll)?1:0;
112          this.mode = (this.mode=="stream")?"stream":"poll";          this.mode = (this.mode=="stream")?"stream":"poll";
113          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";
114            this.stop();
115          var now = new Date();          var now = new Date();
116          var t = now.getTime();          var t = now.getTime();
117          if (typeof(this.transferDoc)=="object") {          this.setstatus(1);
118                  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));  
         }  
119          if (this.mode=="stream") {          if (this.mode=="stream") {
120                  if (document.all) {                  if (document.all) {
121                          this.setstatus(1);                          this.createIframe(this.subsurl);
                         this.transferDoc = new ActiveXObject("htmlfile");  
                         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>";  
122                  } else {                  } else {
123                          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);  
                         this.frameurl = "http://"+this.subdomain+"."+location.hostname+"/stream.html";  
                         this.frameload();  
124                  }                  }
125                  var f = this.pollmode.bind(this);                  var f = this.pollmode.bind(this);
126                  clearTimeout(this.pingtimer);                  clearTimeout(this.pingtimer);
127                  this.pingtimer = setTimeout(f, this.pingtimeout);                  this.pingtimer = setTimeout(f, this.pingtimeout);
128    
129          } else {          } else {
130                    this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html?nc="+t);
131                    this.recvtimes[0] = t;
132                    if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);
133                    this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);
134            }
135            this.lastrequest = t;
136    }
137    
138    Meteor.prototype.updateSubsUrl = function() {
139    
140            // If streaming or long polling, connection should persist
141            this.persist = (this.mode == "stream" || (this.mode=='poll' && this.maxmessages > 0)) ? 1 : 0;
142            var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId;
143            if (this.persist && this.mode != "stream") surl += "&maxmessages=" + this.maxmessages;
144            surl += "&persist="+this.persist;
145            for (var c in this.channels) {
146                    surl += "&channel="+c;
147                    if (this.channels[c].lastmsgreceived > 0) {
148                            surl += "&restartfrom="+(this.channels[c].lastmsgreceived+1);
149                    } else if (this.channels[c].backtrack > 0) {
150                            surl += "&backtrack="+this.channels[c].backtrack;
151                    } else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) {
152                            surl += "&restartfrom=";
153                    }
154            }
155            this.subsurl = surl;
156    }
157    
158    Meteor.prototype.createIframe = function(url) {
159            delete this.transferDoc;
160            if (document.all) try { this.transferDoc = new ActiveXObject("htmlfile") } catch(ex) { this.transferDoc = null }
161            if (document.all && this.transferDoc) {
162                    this.transferDoc.open();
163                    this.transferDoc.write("<html>");
164                    this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
165                    this.transferDoc.write("</html>");
166                    this.transferDoc.parentWindow.Meteor = Meteor;
167                    this.transferDoc.close();
168                    var ifrDiv = this.transferDoc.createElement("div");
169                    this.transferDoc.appendChild(ifrDiv);
170                    ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"\" style=\"display: none;\"></iframe>";
171            } else {
172                  var ifr = document.createElement("IFRAME");                  var ifr = document.createElement("IFRAME");
173                  ifr.style.width = "10px";                  ifr.style.width = "10px";
174                  ifr.style.height = "10px";                  ifr.style.height = "10px";
175                  ifr.style.border = "none";                  ifr.style.border = "none";
176                  if (document.all) {                  ifr.style.position = "absolute";
177                          ifr.style.display = "none";                  ifr.style.top = "-10px";
178                    ifr.style.marginTop = "-10px";
179                    ifr.style.zIndex = "-20";
180                    ifr.setAttribute("id", "meteorframe_"+this.instID);
181                    ifr.Meteor = Meteor;
182                    if (document.compatMode=='CSS1Compat') {
183                            var innerifr = document.createElement("IFRAME");
184                            innerifr.setAttribute("src", url);
185                            innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
186                            ifr.appendChild(innerifr);
187                            document.body.appendChild(ifr);
188                  } else {                  } else {
189                          ifr.style.position = "absolute";                          ifr.setAttribute("src", url);
190                          ifr.style.marginTop = "-10px";                          document.body.appendChild(ifr);
                         ifr.style.zIndex = "-20";  
191                  }                  }
                 ifr.id = "meteorframe_"+this.instID;  
                 document.body.appendChild(ifr);  
                 this.frameurl = "http://"+this.subdomain+"."+location.hostname+"/poll.html";  
                 this.frameload();  
                 this.recvtimes[0] = t;  
                 if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);  
                 this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);  
192          }          }
193          this.lastrequest = t;          if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
194            var f = this.frameloadtimeout.bind(this);
195            this.frameloadtimer = setTimeout(f, 5000);
196    }
197    
198    Meteor.prototype.stop = function() {
199            if (typeof(this.transferDoc)=="object") {
200                    this.transferDoc = false;
201            }
202            if (document.getElementById("meteorframe_"+this.instID)) {
203                    document.getElementById("meteorframe_"+this.instID).src="about:blank";
204                    document.body.removeChild(document.getElementById("meteorframe_"+this.instID));
205            }
206            clearTimeout(this.pingtimer);
207            clearTimeout(this.updatepollfreqtimer);
208            clearTimeout(this.frameloadtimer);
209            this.setstatus(0);
210  }  }
211    
212  Meteor.prototype.pollmode = function() {  Meteor.prototype.pollmode = function() {
213            if (this.debugmode) console.log("Ping timeout");
214          this.mode="poll";          this.mode="poll";
215          this.start();          this.start();
216          this.callback_changemode("poll");          this.callback_changemode("poll");
217          this.lastpingtime = false;          this.lastpingtime = false;
218  }  }
219    
220  Meteor.prototype.process = function(id, data) {  Meteor.prototype.process = function(id, channel, data) {
221          if (id > this.lastmsgreceived) {          if (id == -1) {
222                    if (this.debugmode) console.log("Ping");
223                    this.ping();
224            } else if (typeof(this.channels[channel]) != "undefined" && id > this.channels[channel].lastmsgreceived) {
225                    if (this.debugmode) console.log("Message "+id+" received on channel "+channel+" (last id on channel: "+this.channels[channel].lastmsgreceived+")\n"+data);
226                  this.callback_process(data);                  this.callback_process(data);
227                  if (id != -1) this.lastmsgreceived = id;                  this.channels[channel].lastmsgreceived = id;
228                  if (this.mode=="poll") {                  if (this.mode=="poll") {
229                          var now = new Date();                          var now = new Date();
230                          var t = now.getTime();                          var t = now.getTime();
231                          this.recvtimes[this.recvtimes.length] = t;                          this.recvtimes[this.recvtimes.length] = t;
232                          while (this.recvtimes.length > 5) this.recvtimes.shift();                          while (this.recvtimes.length > 5) this.recvtimes.shift();
233                  }                  }
         } else if (id == -1) {  
                 this.ping();  
234          }          }
235            this.updateSubsUrl();
236          this.setstatus(5);          this.setstatus(5);
237  }  }
238    
# Line 220  Meteor.prototype.ping = function() { Line 248  Meteor.prototype.ping = function() {
248  }  }
249    
250  Meteor.prototype.reset = function() {  Meteor.prototype.reset = function() {
251            if (this.debugmode) console.log("Stream reset");
252          var now = new Date();          var now = new Date();
253          var t = now.getTime();          var t = now.getTime();
254          var x = this.pollfreq - (t-this.lastrequest);          var x = this.pollfreq - (t-this.lastrequest);
# Line 274  Meteor.prototype.registerEventCallback = Line 303  Meteor.prototype.registerEventCallback =
303          }          }
304  }  }
305    
 Meteor.prototype.frameload = function() {  
         this.setstatus(2);  
         if (document.getElementById("meteorframe_"+this.instID)) {  
                 var f = this.frameloadtimeout.bind(this);  
                 this.frameloadtimer = setTimeout(f, 5000);  
                 document.getElementById("meteorframe_"+this.instID).src = "about:blank";  
                 setTimeout(this.doloadurl.bind(this), 100);  
         }  
 }  
 Meteor.prototype.doloadurl = function() {  
         var now = new Date();  
         var t = now.getTime();  
         document.getElementById("meteorframe_"+this.instID).src = this.frameurl+"?nocache="+t;  
 }  
306  Meteor.prototype.frameloadtimeout = function() {  Meteor.prototype.frameloadtimeout = function() {
307            if (this.debugmode) console.log("Frame load timeout");
308          if (this.frameloadtimer) clearTimeout(this.frameloadtimer);          if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
309          this.setstatus(3);          this.setstatus(3);
310          this.frameload();          setTimeout(this.start.bind(this), 5000);
311  }  }
312  Meteor.prototype.setstatus = function(newstatus) {  Meteor.prototype.setstatus = function(newstatus) {
313          if (this.status != newstatus) {          if (this.status != newstatus) {
# Line 300  Meteor.prototype.setstatus = function(ne Line 316  Meteor.prototype.setstatus = function(ne
316          }          }
317  }  }
318    
319    
320  Meteor.createCookie = function(name,value,days) {  Meteor.createCookie = function(name,value,days) {
321          if (days) {          if (days) {
322                  var date = new Date();                  var date = new Date();
# Line 323  Meteor.readCookie = function(name) { Line 340  Meteor.readCookie = function(name) {
340    
341  Meteor.eraseCookie = function(name) {  Meteor.eraseCookie = function(name) {
342          createCookie(name,"",-1);          createCookie(name,"",-1);
 }  
343    }

Legend:
Removed from v.7  
changed lines
  Added in v.28

  ViewVC Help
Powered by ViewVC 1.1.26