/[meteor]/trunk/public_html/meteor.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/public_html/meteor.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 6 by andrew.betts, Tue Nov 21 09:49:07 2006 UTC revision 19 by andrew.betts, Wed May 2 11:21:14 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() {};
33          this.persist = true;          this.persist = true;
34            this.frameloadtimer = false;
35            this.debugmode = false;
36            this.subsurl = false;
37            this.channels = {};
38    
39          // Documented public properties          // Documented public properties
         this.channel = false;  
40          this.subdomain = "data";          this.subdomain = "data";
41          this.dynamicpageaddress = "push";          this.dynamicpageaddress = "push";
         this.backtrack = 0;  
42          this.smartpoll = true;          this.smartpoll = true;
43          this.pollfreq = 2000;          this.pollfreq = 2000;
44          this.minpollfreq = 2000;          this.minpollfreq = 2000;
45          this.mode = "stream";          this.mode = "poll";
46          this.polltimeout=30000;          this.polltimeout=30000;
         this.maxmessages=0;  
47          this.pingtimeout = 10000;          this.pingtimeout = 10000;
48            this.maxmessages = 0;
49            this.status = 0;
50    
51            /* Statuses:    0 = Uninitialised,
52                                            1 = Loading stream,
53                                            2 = Loading controller frame,
54                                            3 = Controller frame timeout, retrying.
55                                            4 = Controller frame loaded and ready
56                                            5 = Receiving data
57            */
58    
         // 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);  
         }  
59          this.instID = (typeof(instID) != "undefined") ? instID : 0;          this.instID = (typeof(instID) != "undefined") ? instID : 0;
60            this.MHostId = Math.floor(Math.random()*100000000)+""+this.instID;
61  }  }
62    
63  Meteor.instances = new Array();  Meteor.instances = new Array();
 Meteor.servertimeoffset = 0;  
64    
65  Meteor.create = function(instID) {  Meteor.create = function(instID) {
66          if (!instID) instID = 0;          if (!instID) instID = Meteor.instances.length;
67          Meteor.instances[instID] = new Meteor(instID);          Meteor.instances[instID] = new Meteor(instID);
68          return Meteor.instances[instID];          return Meteor.instances[instID];
69  }  }
70    
71  Meteor.register = function(ifr) {  Meteor.register = function(ifr) {
72          instid = new String(ifr.window.frameElement.id);          instid = new String(ifr.window.frameElement.id);
73          instid = instid.replace("meteorframe_", "");          instid = instid.replace(/.*_([0-9]*)$/, "$1");
74          ifr.p = this.instances[instid].process.bind(this.instances[instid]);          ifr.p = this.instances[instid].process.bind(this.instances[instid]);
75          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);
76          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);
77          ifr.get = this.instances[instid].get.bind(this.instances[instid]);          ifr.get = this.instances[instid].get.bind(this.instances[instid]);
78          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);
79            clearTimeout(this.instances[instid].frameloadtimer);
80            this.instances[instid].setstatus(4);
81            if (this.debugmode) console.log("Frame registered");
82  }  }
83    
84  Meteor.setServerTime = function(timestamp) {  Meteor.reset = function(ifr) {
85          var now = new Date();          instid = new String(ifr.window.frameElement.id);
86          var clienttime = (now.getTime() / 1000);          instid = instid.replace(/.*_([0-9]*)$/, "$1");
87          Meteor.servertimeoffset = timestamp - clienttime;          this.instances[instid].reset();
88    }
89    
90    Meteor.prototype.joinChannel = function(channelname, backtrack) {
91            if (typeof(this.channels[channelname]) != "undefined") throw "Cannot join channel "+channelname+": already subscribed";
92            this.channels[channelname] = {backtrack:backtrack, lastmsgreceived:0};
93            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    }
97    
98    Meteor.prototype.leaveChannel = function(channelname) {
99            if (typeof(this.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed";
100            delete this.channels[channelname];
101            if (this.status != 0) this.start();
102  }  }
103    
104  Meteor.prototype.start = function() {  Meteor.prototype.start = function() {
105          this.persist = (this.maxmessages)?1:0;          this.persist = (this.maxmessages)?1:0;
106          this.smartpoll = (this.smartpoll)?1:0;          this.smartpoll = (this.smartpoll)?1:0;
107          this.mode = (this.mode=="stream")?"stream":"poll";          this.mode = (this.mode=="stream")?"stream":"poll";
108          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";          if (!this.subdomain || this.channels.length) throw "Channel or Meteor subdomain host not specified";
109            this.stop();
110          var now = new Date();          var now = new Date();
111          var t = now.getTime();          var t = now.getTime();
112          if (typeof(this.transferDoc)=="object") {          this.setstatus(1);
113                  this.transferDoc.open();          var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId;
114                  this.transferDoc.close();          if (this.maxmessages && !this.persist) surl += "&maxmessages=" + this.maxmessages;
115                  delete this.transferDoc;          for (var c in this.channels) {
116          }                  surl += "&channel="+c;
117          if (document.getElementById("meteorframe_"+this.instID)) {                  if (this.channels[c].lastmsgreceived >= 0) {
118                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));                          surl += "&restartfrom="+this.channels[c].lastmsgreceived;
119                    } else if (this.channels[c].backtrack > 0) {
120                            surl += "&backtrack="+this.channels[c].backtrack;
121                    } else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) {
122                            surl += "&restartfrom=";
123                    }
124          }          }
125            this.subsurl = surl;
126          if (this.mode=="stream") {          if (this.mode=="stream") {
127                  if (document.all) {                  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>";  
                 } else {  
                         var ifr = document.createElement("IFRAME");  
                         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;  
                 }  
128                  var f = this.pollmode.bind(this);                  var f = this.pollmode.bind(this);
129                  clearTimeout(this.pingtimer);                  clearTimeout(this.pingtimer);
130                  this.pingtimer = setTimeout(f, this.pingtimeout);                  this.pingtimer = setTimeout(f, this.pingtimeout);
131    
132          } else {          } else {
133                    this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html");
134                    this.recvtimes[0] = t;
135                    if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);
136                    this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);
137            }
138            this.lastrequest = t;
139    }
140    
141    Meteor.prototype.createIframe = function(url) {
142            if (document.all) {
143                    this.transferDoc = new ActiveXObject("htmlfile");
144                    this.transferDoc.open();
145                    this.transferDoc.write("<html>");
146                    this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
147                    this.transferDoc.write("</html>");
148                    var selfref = this;
149                    this.transferDoc.parentWindow.Meteor = Meteor;
150                    this.transferDoc.close();
151                    var ifrDiv = this.transferDoc.createElement("div");
152                    this.transferDoc.appendChild(ifrDiv);
153                    ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"\" style=\"display: none;\"></iframe>";
154            } else {
155                  var ifr = document.createElement("IFRAME");                  var ifr = document.createElement("IFRAME");
156                  ifr.style.width = "10px";                  ifr.style.width = "10px";
157                  ifr.style.height = "10px";                  ifr.style.height = "10px";
158                  ifr.style.border = "none";                  ifr.style.border = "none";
159                  if (document.all) {                  ifr.style.position = "absolute";
160                          ifr.style.display = "none";                  ifr.style.top = "-10px";
161                  } else {                  ifr.style.marginTop = "-10px";
162                          ifr.style.position = "absolute";                  ifr.style.zIndex = "-20";
163                          ifr.style.marginTop = "-10px";                  ifr.setAttribute("id", "meteorframe_"+this.instID);
164                          ifr.style.zIndex = "-20";                  ifr.Meteor = Meteor;
165                  }                  var innerifr = document.createElement("IFRAME");
166                  ifr.id = "meteorframe_"+this.instID;                  innerifr.setAttribute("src", url);
167                    innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
168                    ifr.appendChild(innerifr);
169                  document.body.appendChild(ifr);                  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);  
170          }          }
171          this.lastrequest = t;          if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
172            var f = this.frameloadtimeout.bind(this);
173            this.frameloadtimer = setTimeout(f, 5000);
174    }
175    
176    Meteor.prototype.stop = function() {
177            if (typeof(this.transferDoc)=="object") {
178                    this.transferDoc = false;
179            }
180            if (document.getElementById("meteorframe_"+this.instID)) {
181                    document.getElementById("meteorframe_"+this.instID).src="about:blank";
182                    document.body.removeChild(document.getElementById("meteorframe_"+this.instID));
183            }
184            if (!isNaN(this.pingtimer)) clearTimeout(this.pingtimer);
185            if (!isNaN(this.updatepollfreqtimer)) clearTimeout(this.updatepollfreqtimer);
186            if (!isNaN(this.frameloadtimer)) clearTimeout(this.frameloadtimer);
187            this.setstatus(0);
188  }  }
189    
190  Meteor.prototype.pollmode = function() {  Meteor.prototype.pollmode = function() {
191            if (this.debugmode) console.log("Ping timeout");
192          this.mode="poll";          this.mode="poll";
193          this.start();          this.start();
194          this.callback_changemode("poll");          this.callback_changemode("poll");
195            this.lastpingtime = false;
196  }  }
197    
198  Meteor.prototype.process = function(id, data) {  Meteor.prototype.process = function(id, channel, data) {
199          if (id > this.lastmsgreceived) {          if (id == -1) {
200                    if (this.debugmode) console.log("Ping");
201                    this.ping();
202            } else if (typeof(this.channels[channel]) != "undefined" && id > this.channels[channel].lastmsgreceived) {
203                    if (this.debugmode) console.log("Message "+id+" received on channel "+channel+" (last id on channel: "+this.channels[channel].lastmsgreceived+")\n"+data);
204                  this.callback_process(data);                  this.callback_process(data);
205                  if (id != -1) this.lastmsgreceived = id;                  this.channels[channel].lastmsgreceived = id;
206                  if (this.mode=="poll") {                  if (this.mode=="poll") {
207                          var now = new Date();                          var now = new Date();
208                          var t = now.getTime();                          var t = now.getTime();
209                          this.recvtimes[this.recvtimes.length] = t;                          this.recvtimes[this.recvtimes.length] = t;
210                          while (this.recvtimes.length > 5) this.recvtimes.shift();                          while (this.recvtimes.length > 5) this.recvtimes.shift();
211                  }                  }
         } else if (id == -1) {  
                 this.ping();  
212          }          }
213            this.setstatus(5);
214  }  }
215    
216  Meteor.prototype.ping = function() {  Meteor.prototype.ping = function() {
# Line 197  Meteor.prototype.ping = function() { Line 221  Meteor.prototype.ping = function() {
221                  var now = new Date();                  var now = new Date();
222                  this.lastpingtime = now.getTime();                  this.lastpingtime = now.getTime();
223          }          }
224            this.setstatus(5);
225  }  }
226    
227  Meteor.prototype.reset = function() {  Meteor.prototype.reset = function() {
228            if (this.debugmode) console.log("Stream reset");
229          var now = new Date();          var now = new Date();
230          var t = now.getTime();          var t = now.getTime();
231          var x = this.pollfreq - (t-this.lastrequest);          var x = this.pollfreq - (t-this.lastrequest);
# Line 249  Meteor.prototype.registerEventCallback = Line 275  Meteor.prototype.registerEventCallback =
275                  this.callback_eof = (this.callback_eof).andThen(funcRef);                  this.callback_eof = (this.callback_eof).andThen(funcRef);
276          } else if (evt=="changemode") {          } else if (evt=="changemode") {
277                  this.callback_changemode = (this.callback_changemode).andThen(funcRef);                  this.callback_changemode = (this.callback_changemode).andThen(funcRef);
278            } else if (evt=="changestatus") {
279                    this.callback_statuschanged = (this.callback_statuschanged).andThen(funcRef);
280          }          }
281  }  }
282    
283    Meteor.prototype.frameloadtimeout = function() {
284            if (this.debugmode) console.log("Frame load timeout");
285            if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
286            this.setstatus(3);
287            setTimeout(this.start.bind(this), 5000);
288    }
289    Meteor.prototype.setstatus = function(newstatus) {
290            if (this.status != newstatus) {
291                    this.status = newstatus;
292                    this.callback_statuschanged(newstatus);
293            }
294    }
295    
296  Meteor.createCookie = function(name,value,days) {  Meteor.createCookie = function(name,value,days) {
297          if (days) {          if (days) {

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

  ViewVC Help
Powered by ViewVC 1.1.26