/[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 13 by andrew.betts, Thu Dec 14 10:45:43 2006 UTC revision 14 by andrew.betts, Mon Apr 30 18:38:55 2007 UTC
# Line 16  Function.prototype.andThen=function(g) { Line 16  Function.prototype.andThen=function(g) {
16                  f(a);g(args);                  f(a);g(args);
17          }          }
18  };  };
 function addUnLoadEvent(func) {  
   var oldonunload = window.onunload;  
   if (typeof window.onunload != 'function') {  
     window.onunload = func;  
   } else {  
     window.onunload = function() {  
       if (oldonunload) {  
         oldonunload();  
       }  
       func();  
     }  
   }  
 }  
 //addUnLoadEvent(meteordestroy);  
 function meteordestroy() {  
         var x = Meteor.instances.length;  
         for(var i=0; i<x; i++) {  
                 Meteor.instances[i].stop();  
                 delete Meteor.instances[i];  
         }  
 }  
19    
20  function Meteor(instID) {  function Meteor(instID) {
21    
# Line 55  function Meteor(instID) { Line 34  function Meteor(instID) {
34          this.persist = true;          this.persist = true;
35          this.frameloadtimer = false;          this.frameloadtimer = false;
36          this.frameurl = false;          this.frameurl = false;
37            this.debugmode = false;
38    
39          // Documented public properties          // Documented public properties
40          this.channel = false;          this.channel = false;
# Line 64  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;
# Line 73  function Meteor(instID) { Line 53  function Meteor(instID) {
53          /* Statuses:    0 = Uninitialised,          /* Statuses:    0 = Uninitialised,
54                                          1 = Loading stream,                                          1 = Loading stream,
55                                          2 = Loading controller frame,                                          2 = Loading controller frame,
56                                          3 = Controller frame timeout, retrying every 5 seconds                                          3 = Controller frame timeout, retrying.
57                                          4 = Controller frame loaded and ready                                          4 = Controller frame loaded and ready
58                                          5 = Receiving data                                          5 = Receiving data
59          */          */
60    
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;          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 = Meteor.instances.length;          if (!instID) instID = Meteor.instances.length;
# Line 93  Meteor.create = function(instID) { Line 72  Meteor.create = function(instID) {
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]);
# Line 101  Meteor.register = function(ifr) { Line 80  Meteor.register = function(ifr) {
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);          clearTimeout(this.instances[instid].frameloadtimer);
82          this.instances[instid].setstatus(4);          this.instances[instid].setstatus(4);
83  }          if (this.debugmode) console.log("Frame registered");
   
 Meteor.setServerTime = function(timestamp) {  
         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 114  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.setstatus(1);                  if (this.lastmsgreceived >= 0) {
98                          this.transferDoc = new ActiveXObject("htmlfile");                          surl += "&restartfrom="+this.lastmsgreceived;
99                          this.transferDoc.open();                  } else if (this.backtrack > 0) {
100                          this.transferDoc.write("<html>");                          surl += "&backtrack="+this.backtrack;
101                          this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");                  } else if (this.backtrack < 0 || isNaN(this.backtrack)) {
102                          this.transferDoc.write("</html>");                          surl += "&restartfrom=";
                         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);  
                         this.frameurl = "http://"+this.subdomain+"."+location.hostname+"/stream.html";  
                         this.frameload();  
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);
                 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);  
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() {  Meteor.prototype.stop = function() {
154          if (typeof(this.transferDoc)=="object") {          if (typeof(this.transferDoc)=="object") {
155                  this.transferDoc.open();                  this.transferDoc = false;
                 this.transferDoc.close();  
                 delete this.transferDoc;  
156          }          }
157          if (document.getElementById("meteorframe_"+this.instID)) {          if (document.getElementById("meteorframe_"+this.instID)) {
158                  document.getElementById("meteorframe_"+this.instID).src="about:blank";                  document.getElementById("meteorframe_"+this.instID).src="about:blank";
# Line 204  Meteor.prototype.stop = function() { Line 165  Meteor.prototype.stop = function() {
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;          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 238  Meteor.prototype.ping = function() { Line 201  Meteor.prototype.ping = function() {
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 292  Meteor.prototype.registerEventCallback = Line 256  Meteor.prototype.registerEventCallback =
256          }          }
257  }  }
258    
 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;  
 }  
259  Meteor.prototype.frameloadtimeout = function() {  Meteor.prototype.frameloadtimeout = function() {
260            if (this.debugmode) console.log("Frame load timeout");
261          if (this.frameloadtimer) clearTimeout(this.frameloadtimer);          if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
262          this.setstatus(3);          this.setstatus(3);
263          this.frameload();          setTimeout(this.start.bind(this), 5000);
264  }  }
265  Meteor.prototype.setstatus = function(newstatus) {  Meteor.prototype.setstatus = function(newstatus) {
266          if (this.status != newstatus) {          if (this.status != newstatus) {

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

  ViewVC Help
Powered by ViewVC 1.1.26