/[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 14 by andrew.betts, Mon Apr 30 18:38:55 2007 UTC
# Line 30  function Meteor(instID) { Line 30  function Meteor(instID) {
30          this.callback_reset = function() {};          this.callback_reset = function() {};
31          this.callback_eof = function() {};          this.callback_eof = function() {};
32          this.callback_changemode = function() {};          this.callback_changemode = function() {};
33            this.callback_statuschanged = function() {};
34          this.persist = true;          this.persist = true;
35            this.frameloadtimer = false;
36            this.frameurl = false;
37            this.debugmode = false;
38    
39          // Documented public properties          // Documented public properties
40          this.channel = false;          this.channel = false;
# Line 40  function Meteor(instID) { Line 44  function Meteor(instID) {
44          this.smartpoll = true;          this.smartpoll = true;
45          this.pollfreq = 2000;          this.pollfreq = 2000;
46          this.minpollfreq = 2000;          this.minpollfreq = 2000;
47          this.mode = "stream";          this.mode = "poll";
48          this.polltimeout=30000;          this.polltimeout=30000;
49          this.maxmessages=0;          this.maxmessages=0;
50          this.pingtimeout = 10000;          this.pingtimeout = 10000;
51            this.status = 0;
52    
53            /* Statuses:    0 = Uninitialised,
54                                            1 = Loading stream,
55                                            2 = Loading controller frame,
56                                            3 = Controller frame timeout, retrying.
57                                            4 = Controller frame loaded and ready
58                                            5 = Receiving data
59            */
60    
         // 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);  
         }  
61          this.instID = (typeof(instID) != "undefined") ? instID : 0;          this.instID = (typeof(instID) != "undefined") ? instID : 0;
62            this.MHostId = Math.floor(Math.random()*100000000)+""+this.instID;
63  }  }
64    
65  Meteor.instances = new Array();  Meteor.instances = new Array();
 Meteor.servertimeoffset = 0;  
66    
67  Meteor.create = function(instID) {  Meteor.create = function(instID) {
68          if (!instID) instID = 0;          if (!instID) instID = Meteor.instances.length;
69          Meteor.instances[instID] = new Meteor(instID);          Meteor.instances[instID] = new Meteor(instID);
70          return Meteor.instances[instID];          return Meteor.instances[instID];
71  }  }
72    
73  Meteor.register = function(ifr) {  Meteor.register = function(ifr) {
74          instid = new String(ifr.window.frameElement.id);          instid = new String(ifr.window.frameElement.id);
75          instid = instid.replace("meteorframe_", "");          instid = instid.replace(/.*_([0-9]*)$/, "$1");
76          ifr.p = this.instances[instid].process.bind(this.instances[instid]);          ifr.p = this.instances[instid].process.bind(this.instances[instid]);
77          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);          ifr.r = this.instances[instid].reset.bind(this.instances[instid]);
78          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);          ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);
79          ifr.get = this.instances[instid].get.bind(this.instances[instid]);          ifr.get = this.instances[instid].get.bind(this.instances[instid]);
80          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);          ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);
81  }          clearTimeout(this.instances[instid].frameloadtimer);
82            this.instances[instid].setstatus(4);
83  Meteor.setServerTime = function(timestamp) {          if (this.debugmode) console.log("Frame registered");
         var now = new Date();  
         var clienttime = (now.getTime() / 1000);  
         Meteor.servertimeoffset = timestamp - clienttime;  
84  }  }
85    
86  Meteor.prototype.start = function() {  Meteor.prototype.start = function() {
# Line 98  Meteor.prototype.start = function() { Line 88  Meteor.prototype.start = function() {
88          this.smartpoll = (this.smartpoll)?1:0;          this.smartpoll = (this.smartpoll)?1:0;
89          this.mode = (this.mode=="stream")?"stream":"poll";          this.mode = (this.mode=="stream")?"stream":"poll";
90          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";          if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";
91            this.stop();
92          var now = new Date();          var now = new Date();
93          var t = now.getTime();          var t = now.getTime();
94          if (typeof(this.transferDoc)=="object") {          this.setstatus(1);
                 this.transferDoc.open();  
                 this.transferDoc.close();  
                 delete this.transferDoc;  
         }  
         if (document.getElementById("meteorframe_"+this.instID)) {  
                 document.body.removeChild(document.getElementById("meteorframe_"+this.instID));  
         }  
95          if (this.mode=="stream") {          if (this.mode=="stream") {
96                  if (document.all) {                  var surl = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId;
97                          this.transferDoc = new ActiveXObject("htmlfile");                  if (this.lastmsgreceived >= 0) {
98                          this.transferDoc.open();                          surl += "&restartfrom="+this.lastmsgreceived;
99                          this.transferDoc.write("<html>");                  } else if (this.backtrack > 0) {
100                          this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");                          surl += "&backtrack="+this.backtrack;
101                          this.transferDoc.write("</html>");                  } else if (this.backtrack < 0 || isNaN(this.backtrack)) {
102                          var selfref = this;                          surl += "&restartfrom=";
                         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;  
103                  }                  }
104                    this.createIframe(surl);
105                  var f = this.pollmode.bind(this);                  var f = this.pollmode.bind(this);
106                  clearTimeout(this.pingtimer);                  clearTimeout(this.pingtimer);
107                  this.pingtimer = setTimeout(f, this.pingtimeout);                  this.pingtimer = setTimeout(f, this.pingtimeout);
108    
109          } else {          } else {
110                    this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html");
111                    this.recvtimes[0] = t;
112                    if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);
113                    this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);
114            }
115            this.lastrequest = t;
116    }
117    
118    Meteor.prototype.createIframe = function(url) {
119            if (document.all) {
120                    this.transferDoc = new ActiveXObject("htmlfile");
121                    this.transferDoc.open();
122                    this.transferDoc.write("<html>");
123                    this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
124                    this.transferDoc.write("</html>");
125                    var selfref = this;
126                    this.transferDoc.parentWindow.Meteor = Meteor;
127                    this.transferDoc.close();
128                    var ifrDiv = this.transferDoc.createElement("div");
129                    this.transferDoc.appendChild(ifrDiv);
130                    ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"\" style=\"display: none;\"></iframe>";
131            } else {
132                  var ifr = document.createElement("IFRAME");                  var ifr = document.createElement("IFRAME");
133                  ifr.style.width = "10px";                  ifr.style.width = "10px";
134                  ifr.style.height = "10px";                  ifr.style.height = "10px";
135                  ifr.style.border = "none";                  ifr.style.border = "none";
136                  if (document.all) {                  ifr.style.position = "absolute";
137                          ifr.style.display = "none";                  ifr.style.top = "-10px";
138                  } else {                  ifr.style.marginTop = "-10px";
139                          ifr.style.position = "absolute";                  ifr.style.zIndex = "-20";
140                          ifr.style.marginTop = "-10px";                  ifr.setAttribute("id", "meteorframe_"+this.instID);
141                          ifr.style.zIndex = "-20";                  ifr.Meteor = Meteor;
142                  }                  var innerifr = document.createElement("IFRAME");
143                  ifr.id = "meteorframe_"+this.instID;                  innerifr.setAttribute("src", url);
144                    innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
145                    ifr.appendChild(innerifr);
146                  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);  
147          }          }
148          this.lastrequest = t;          if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
149            var f = this.frameloadtimeout.bind(this);
150            this.frameloadtimer = setTimeout(f, 5000);
151    }
152    
153    Meteor.prototype.stop = function() {
154            if (typeof(this.transferDoc)=="object") {
155                    this.transferDoc = false;
156            }
157            if (document.getElementById("meteorframe_"+this.instID)) {
158                    document.getElementById("meteorframe_"+this.instID).src="about:blank";
159                    document.body.removeChild(document.getElementById("meteorframe_"+this.instID));
160            }
161            if (!isNaN(this.pingtimer)) clearTimeout(this.pingtimer);
162            if (!isNaN(this.updatepollfreqtimer)) clearTimeout(this.updatepollfreqtimer);
163            if (!isNaN(this.frameloadtimer)) clearTimeout(this.frameloadtimer);
164            this.setstatus(0);
165  }  }
166    
167  Meteor.prototype.pollmode = function() {  Meteor.prototype.pollmode = function() {
168            if (this.debugmode) console.log("Ping timeout");
169            this.stop();
170          this.mode="poll";          this.mode="poll";
171          this.start();          this.start();
172          this.callback_changemode("poll");          this.callback_changemode("poll");
173            this.lastpingtime = false;
174  }  }
175    
176  Meteor.prototype.process = function(id, data) {  Meteor.prototype.process = function(id, data, timestamp) {
177          if (id > this.lastmsgreceived) {          if (id > this.lastmsgreceived) {
178                  this.callback_process(data);                  this.callback_process(data, timestamp);
179                  if (id != -1) this.lastmsgreceived = id;                  if (id != -1) this.lastmsgreceived = id;
180                  if (this.mode=="poll") {                  if (this.mode=="poll") {
181                          var now = new Date();                          var now = new Date();
# Line 187  Meteor.prototype.process = function(id, Line 186  Meteor.prototype.process = function(id,
186          } else if (id == -1) {          } else if (id == -1) {
187                  this.ping();                  this.ping();
188          }          }
189            this.setstatus(5);
190  }  }
191    
192  Meteor.prototype.ping = function() {  Meteor.prototype.ping = function() {
# Line 197  Meteor.prototype.ping = function() { Line 197  Meteor.prototype.ping = function() {
197                  var now = new Date();                  var now = new Date();
198                  this.lastpingtime = now.getTime();                  this.lastpingtime = now.getTime();
199          }          }
200            this.setstatus(5);
201  }  }
202    
203  Meteor.prototype.reset = function() {  Meteor.prototype.reset = function() {
204            if (this.debugmode) console.log("Stream reset");
205          var now = new Date();          var now = new Date();
206          var t = now.getTime();          var t = now.getTime();
207          var x = this.pollfreq - (t-this.lastrequest);          var x = this.pollfreq - (t-this.lastrequest);
# Line 249  Meteor.prototype.registerEventCallback = Line 251  Meteor.prototype.registerEventCallback =
251                  this.callback_eof = (this.callback_eof).andThen(funcRef);                  this.callback_eof = (this.callback_eof).andThen(funcRef);
252          } else if (evt=="changemode") {          } else if (evt=="changemode") {
253                  this.callback_changemode = (this.callback_changemode).andThen(funcRef);                  this.callback_changemode = (this.callback_changemode).andThen(funcRef);
254            } else if (evt=="changestatus") {
255                    this.callback_statuschanged = (this.callback_statuschanged).andThen(funcRef);
256          }          }
257  }  }
258    
259    Meteor.prototype.frameloadtimeout = function() {
260            if (this.debugmode) console.log("Frame load timeout");
261            if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
262            this.setstatus(3);
263            setTimeout(this.start.bind(this), 5000);
264    }
265    Meteor.prototype.setstatus = function(newstatus) {
266            if (this.status != newstatus) {
267                    this.status = newstatus;
268                    this.callback_statuschanged(newstatus);
269            }
270    }
271    
272  Meteor.createCookie = function(name,value,days) {  Meteor.createCookie = function(name,value,days) {
273          if (days) {          if (days) {

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

  ViewVC Help
Powered by ViewVC 1.1.26