/[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 18 by andrew.betts, Wed May 2 10:55:26 2007 UTC revision 29 by andrew.betts, Wed Oct 10 22:18:30 2007 UTC
# Line 23  function Meteor(instID) { Line 23  function Meteor(instID) {
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.debugmode = false;          this.debugmode = false;
36          this.subsurl = false;          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.channels = new Array();  
43          this.subdomain = "data";          this.subdomain = "data";
44          this.dynamicpageaddress = "push";          this.dynamicpageaddress = "push";
45          this.smartpoll = true;          this.smartpoll = 1;
46          this.pollfreq = 2000;          this.pollfreq = 2000;
47          this.minpollfreq = 2000;          this.minpollfreq = 2000;
48          this.mode = "poll";          this.mode = "stream";
49          this.polltimeout=30000;          this.polltimeout = 30000;
50          this.pingtimeout = 10000;          this.pingtimeout = 10000;
51          this.maxmessages = 0;          this.maxmessages = 0;
52          this.status = 0;          this.status = 0;
# Line 87  Meteor.reset = function(ifr) { Line 90  Meteor.reset = function(ifr) {
90          this.instances[instid].reset();          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) {  Meteor.prototype.joinChannel = function(channelname, backtrack) {
101          if (typeof(this.channels[channelname]) != "undefined") throw "Cannot join channel "+channelname+": already subscribed";          if (typeof(this.channels[channelname]) != "undefined") throw "Cannot join channel "+channelname+": already subscribed";
102          this.channels[channelname] = {backtrack:backtrack, lastmsgreceived:0};          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();          if (this.status != 0) this.start();
106            this.channelcount++;
107  }  }
108    
109  Meteor.prototype.leaveChannel = function(channelname) {  Meteor.prototype.leaveChannel = function(channelname) {
110          if (typeof(this.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed";          if (typeof(this.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed";
111          delete this.channels[channelname];          delete this.channels[channelname];
112          if (this.status != 0) this.start();          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.channels.length) throw "Channel or Meteor subdomain host not specified";          if (!this.subdomain || !this.channelcount) throw "Channel or Meteor subdomain host not specified";
121          this.stop();          this.stop();
122          var now = new Date();          var now = new Date();
123          var t = now.getTime();          var t = now.getTime();
124          this.setstatus(1);          this.setstatus(1);
125          var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId;          this.updateSubsUrl();
         if (this.maxmessages && !this.persist) surl += "&maxmessages=" + this.maxmessages;  
         for (var c in this.channels) {  
                 surl += "&channel="+c;  
                 if (this.channels[c].lastmsgreceived >= 0) {  
                         surl += "&restartfrom="+this.channels[c].lastmsgreceived;  
                 } else if (this.channels[c].backtrack > 0) {  
                         surl += "&backtrack="+this.channels[c].backtrack;  
                 } else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) {  
                         surl += "&restartfrom=";  
                 }  
         }  
         this.subsurl = surl;  
126          if (this.mode=="stream") {          if (this.mode=="stream") {
127                  this.createIframe(this.subsurl);                  if (document.all) {
128                            this.createIframe(this.subsurl);
129                    } else {
130                            this.createIframe("http://"+this.subdomain+"."+location.hostname+"/stream.html");
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");                  this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html?nc="+t);
138                  this.recvtimes[0] = t;                  this.recvtimes[0] = t;
139                  if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);                  if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);
140                  this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);                  this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);
# Line 136  Meteor.prototype.start = function() { Line 142  Meteor.prototype.start = function() {
142          this.lastrequest = t;          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) {  Meteor.prototype.createIframe = function(url) {
166          if (document.all) {          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 = new ActiveXObject("htmlfile");                  this.transferDoc = new ActiveXObject("htmlfile");
170                  this.transferDoc.open();                  this.transferDoc.open();
171                  this.transferDoc.write("<html>");                  this.transferDoc.write("<html>");
172                  this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");                  this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
173                  this.transferDoc.write("</html>");                  this.transferDoc.write("</html>");
                 var selfref = this;  
174                  this.transferDoc.parentWindow.Meteor = Meteor;                  this.transferDoc.parentWindow.Meteor = Meteor;
175                  this.transferDoc.close();                  this.transferDoc.close();
176                  var ifrDiv = this.transferDoc.createElement("div");                  var ifrDiv = this.transferDoc.createElement("div");
# Line 160  Meteor.prototype.createIframe = function Line 187  Meteor.prototype.createIframe = function
187                  ifr.style.zIndex = "-20";                  ifr.style.zIndex = "-20";
188                  ifr.setAttribute("id", "meteorframe_"+this.instID);                  ifr.setAttribute("id", "meteorframe_"+this.instID);
189                  ifr.Meteor = Meteor;                  ifr.Meteor = Meteor;
190                  var innerifr = document.createElement("IFRAME");                  if (document.compatMode=='CSS1Compat') {
191                  innerifr.setAttribute("src", url);                          var innerifr = document.createElement("IFRAME");
192                  innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);                          innerifr.setAttribute("src", url);
193                  ifr.appendChild(innerifr);                          innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
194                  document.body.appendChild(ifr);                          ifr.appendChild(innerifr);
195                            document.body.appendChild(ifr);
196                    } else {
197                            ifr.setAttribute("src", url);
198                            document.body.appendChild(ifr);
199                    }
200          }          }
201          if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");          if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
202          var f = this.frameloadtimeout.bind(this);          var f = this.frameloadtimeout.bind(this);
# Line 179  Meteor.prototype.stop = function() { Line 211  Meteor.prototype.stop = function() {
211                  document.getElementById("meteorframe_"+this.instID).src="about:blank";                  document.getElementById("meteorframe_"+this.instID).src="about:blank";
212                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));                  document.body.removeChild(document.getElementById("meteorframe_"+this.instID));
213          }          }
214          if (!isNaN(this.pingtimer)) clearTimeout(this.pingtimer);          clearTimeout(this.pingtimer);
215          if (!isNaN(this.updatepollfreqtimer)) clearTimeout(this.updatepollfreqtimer);          clearTimeout(this.updatepollfreqtimer);
216          if (!isNaN(this.frameloadtimer)) clearTimeout(this.frameloadtimer);          clearTimeout(this.frameloadtimer);
217          this.setstatus(0);          this.setstatus(0);
218  }  }
219    
# Line 194  Meteor.prototype.pollmode = function() { Line 226  Meteor.prototype.pollmode = function() {
226  }  }
227    
228  Meteor.prototype.process = function(id, channel, data) {  Meteor.prototype.process = function(id, channel, data) {
229          if (id > this.channels[channel].lastmsgreceived) {          if (id == -1) {
230                  if (this.debugmode) console.log("Message "+id+" received on channel "+channel+": "+data);                  if (this.debugmode) console.log("Ping");
231                    this.ping();
232            } else if (typeof(this.channels[channel]) != "undefined" && id > this.channels[channel].lastmsgreceived) {
233                    if (this.debugmode) console.log("Message "+id+" received on channel "+channel+" (last id on channel: "+this.channels[channel].lastmsgreceived+")\n"+data);
234                  this.callback_process(data);                  this.callback_process(data);
235                  this.channels[channel].lastmsgreceived = id;                  this.channels[channel].lastmsgreceived = id;
236                  if (this.mode=="poll") {                  if (this.mode=="poll") {
# Line 204  Meteor.prototype.process = function(id, Line 239  Meteor.prototype.process = function(id,
239                          this.recvtimes[this.recvtimes.length] = t;                          this.recvtimes[this.recvtimes.length] = t;
240                          while (this.recvtimes.length > 5) this.recvtimes.shift();                          while (this.recvtimes.length > 5) this.recvtimes.shift();
241                  }                  }
         } else if (id == -1) {  
                 if (this.debugmode) console.log("Ping");  
                 this.ping();  
242          }          }
243            this.updateSubsUrl();
244          this.setstatus(5);          this.setstatus(5);
245  }  }
246    
# Line 291  Meteor.prototype.setstatus = function(ne Line 324  Meteor.prototype.setstatus = function(ne
324          }          }
325  }  }
326    
327    
328  Meteor.createCookie = function(name,value,days) {  Meteor.createCookie = function(name,value,days) {
329          if (days) {          if (days) {
330                  var date = new Date();                  var date = new Date();
# Line 314  Meteor.readCookie = function(name) { Line 348  Meteor.readCookie = function(name) {
348    
349  Meteor.eraseCookie = function(name) {  Meteor.eraseCookie = function(name) {
350          createCookie(name,"",-1);          createCookie(name,"",-1);
 }  
351    }

Legend:
Removed from v.18  
changed lines
  Added in v.29

  ViewVC Help
Powered by ViewVC 1.1.26