/[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 14 by andrew.betts, Mon Apr 30 18:38:55 2007 UTC revision 21 by andrew.betts, Mon May 14 13:42:45 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() {};
# Line 33  function Meteor(instID) { Line 32  function Meteor(instID) {
32          this.callback_statuschanged = function() {};          this.callback_statuschanged = function() {};
33          this.persist = true;          this.persist = true;
34          this.frameloadtimer = false;          this.frameloadtimer = false;
         this.frameurl = false;  
35          this.debugmode = 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";
         this.backtrack = 0;  
45          this.smartpoll = true;          this.smartpoll = true;
46          this.pollfreq = 2000;          this.pollfreq = 2000;
47          this.minpollfreq = 2000;          this.minpollfreq = 2000;
48          this.mode = "poll";          this.mode = "poll";
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,
# Line 83  Meteor.register = function(ifr) { Line 84  Meteor.register = function(ifr) {
84          if (this.debugmode) console.log("Frame registered");          if (this.debugmode) console.log("Frame registered");
85  }  }
86    
87    Meteor.reset = function(ifr) {
88            instid = new String(ifr.window.frameElement.id);
89            instid = instid.replace(/.*_([0-9]*)$/, "$1");
90            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.maxmessages)?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();          this.stop();
115          var now = new Date();          var now = new Date();
116          var t = now.getTime();          var t = now.getTime();
117          this.setstatus(1);          this.setstatus(1);
118          if (this.mode=="stream") {          var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId;
119                  var surl = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId;          if (this.maxmessages && !this.persist) surl += "&maxmessages=" + this.maxmessages;
120                  if (this.lastmsgreceived >= 0) {          for (var c in this.channels) {
121                          surl += "&restartfrom="+this.lastmsgreceived;                  surl += "&channel="+c;
122                  } else if (this.backtrack > 0) {                  if (this.channels[c].lastmsgreceived > 0) {
123                          surl += "&backtrack="+this.backtrack;                          surl += "&restartfrom="+this.channels[c].lastmsgreceived;
124                  } else if (this.backtrack < 0 || isNaN(this.backtrack)) {                  } else if (this.channels[c].backtrack > 0) {
125                            surl += "&backtrack="+this.channels[c].backtrack;
126                    } else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) {
127                          surl += "&restartfrom=";                          surl += "&restartfrom=";
128                  }                  }
129                  this.createIframe(surl);          }
130            this.subsurl = surl;
131            if (this.mode=="stream") {
132                    if (document.all) {
133                            this.createIframe(this.subsurl);
134                    } else {
135                            this.createIframe("http://"+this.subdomain+"."+location.hostname+"/stream.html");
136                    }
137                  var f = this.pollmode.bind(this);                  var f = this.pollmode.bind(this);
138                  clearTimeout(this.pingtimer);                  clearTimeout(this.pingtimer);
139                  this.pingtimer = setTimeout(f, this.pingtimeout);                  this.pingtimer = setTimeout(f, this.pingtimeout);
# Line 122  Meteor.prototype.createIframe = function Line 154  Meteor.prototype.createIframe = function
154                  this.transferDoc.write("<html>");                  this.transferDoc.write("<html>");
155                  this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");                  this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
156                  this.transferDoc.write("</html>");                  this.transferDoc.write("</html>");
                 var selfref = this;  
157                  this.transferDoc.parentWindow.Meteor = Meteor;                  this.transferDoc.parentWindow.Meteor = Meteor;
158                  this.transferDoc.close();                  this.transferDoc.close();
159                  var ifrDiv = this.transferDoc.createElement("div");                  var ifrDiv = this.transferDoc.createElement("div");
# Line 139  Meteor.prototype.createIframe = function Line 170  Meteor.prototype.createIframe = function
170                  ifr.style.zIndex = "-20";                  ifr.style.zIndex = "-20";
171                  ifr.setAttribute("id", "meteorframe_"+this.instID);                  ifr.setAttribute("id", "meteorframe_"+this.instID);
172                  ifr.Meteor = Meteor;                  ifr.Meteor = Meteor;
173                  var innerifr = document.createElement("IFRAME");                  if (document.compatMode=='CSS1Compat') {
174                  innerifr.setAttribute("src", url);                          var innerifr = document.createElement("IFRAME");
175                  innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);                          innerifr.setAttribute("src", url);
176                  ifr.appendChild(innerifr);                          innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
177                  document.body.appendChild(ifr);                          ifr.appendChild(innerifr);
178                            document.body.appendChild(ifr);
179                    } else {
180                            ifr.setAttribute("src", url);
181                            document.body.appendChild(ifr);
182                    }
183          }          }
184          if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");          if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
185          var f = this.frameloadtimeout.bind(this);          var f = this.frameloadtimeout.bind(this);
# Line 166  Meteor.prototype.stop = function() { Line 202  Meteor.prototype.stop = function() {
202    
203  Meteor.prototype.pollmode = function() {  Meteor.prototype.pollmode = function() {
204          if (this.debugmode) console.log("Ping timeout");          if (this.debugmode) console.log("Ping timeout");
         this.stop();  
205          this.mode="poll";          this.mode="poll";
206          this.start();          this.start();
207          this.callback_changemode("poll");          this.callback_changemode("poll");
208          this.lastpingtime = false;          this.lastpingtime = false;
209  }  }
210    
211  Meteor.prototype.process = function(id, data, timestamp) {  Meteor.prototype.process = function(id, channel, data) {
212          if (id > this.lastmsgreceived) {          if (id == -1) {
213                  this.callback_process(data, timestamp);                  if (this.debugmode) console.log("Ping");
214                  if (id != -1) this.lastmsgreceived = id;                  this.ping();
215            } else if (typeof(this.channels[channel]) != "undefined" && id > this.channels[channel].lastmsgreceived) {
216                    if (this.debugmode) console.log("Message "+id+" received on channel "+channel+" (last id on channel: "+this.channels[channel].lastmsgreceived+")\n"+data);
217                    this.callback_process(data);
218                    this.channels[channel].lastmsgreceived = id;
219                  if (this.mode=="poll") {                  if (this.mode=="poll") {
220                          var now = new Date();                          var now = new Date();
221                          var t = now.getTime();                          var t = now.getTime();
222                          this.recvtimes[this.recvtimes.length] = t;                          this.recvtimes[this.recvtimes.length] = t;
223                          while (this.recvtimes.length > 5) this.recvtimes.shift();                          while (this.recvtimes.length > 5) this.recvtimes.shift();
224                  }                  }
         } else if (id == -1) {  
                 this.ping();  
225          }          }
226          this.setstatus(5);          this.setstatus(5);
227  }  }
# Line 269  Meteor.prototype.setstatus = function(ne Line 306  Meteor.prototype.setstatus = function(ne
306          }          }
307  }  }
308    
309    
310  Meteor.createCookie = function(name,value,days) {  Meteor.createCookie = function(name,value,days) {
311          if (days) {          if (days) {
312                  var date = new Date();                  var date = new Date();
# Line 292  Meteor.readCookie = function(name) { Line 330  Meteor.readCookie = function(name) {
330    
331  Meteor.eraseCookie = function(name) {  Meteor.eraseCookie = function(name) {
332          createCookie(name,"",-1);          createCookie(name,"",-1);
 }  
333    }

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

  ViewVC Help
Powered by ViewVC 1.1.26