/[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

Annotation of /googlecode.com/svn/trunk/public_html/meteor.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (hide annotations)
Mon Apr 30 18:38:55 2007 UTC (17 years ago) by andrew.betts
File MIME type: application/javascript
File size: 9269 byte(s)
changed the way the iframe streaming works to fix a back button issue in 
FF and generally improve the elegance, also fixed a bug while I was at it.

1 andrew.betts 6 // Set domain at highest level
2     var domainparts = document.domain.split(".");
3     document.domain = domainparts[domainparts.length-2]+"."+domainparts[domainparts.length-1];
4    
5     Function.prototype.bind = function(obj) {
6     var method = this,
7     temp = function() {
8     return method.apply(obj, arguments);
9     };
10     return temp;
11     }
12     Function.prototype.andThen=function(g) {
13     var f=this;
14     var a=this.arguments
15     return function(args) {
16     f(a);g(args);
17     }
18     };
19    
20     function Meteor(instID) {
21    
22     this.lastmsgreceived = -1;
23     this.transferDoc = false;
24     this.pingtimer = false;
25     this.updatepollfreqtimer = false;
26     this.lastrequest = 0;
27     this.recvtimes = new Array();
28     this.MHostId = false;
29     this.callback_process = function() {};
30     this.callback_reset = function() {};
31     this.callback_eof = function() {};
32     this.callback_changemode = function() {};
33 andrew.betts 7 this.callback_statuschanged = function() {};
34 andrew.betts 6 this.persist = true;
35 andrew.betts 7 this.frameloadtimer = false;
36     this.frameurl = false;
37 andrew.betts 14 this.debugmode = false;
38 andrew.betts 6
39     // Documented public properties
40     this.channel = false;
41     this.subdomain = "data";
42     this.dynamicpageaddress = "push";
43     this.backtrack = 0;
44     this.smartpoll = true;
45     this.pollfreq = 2000;
46     this.minpollfreq = 2000;
47 andrew.betts 14 this.mode = "poll";
48 andrew.betts 6 this.polltimeout=30000;
49     this.maxmessages=0;
50     this.pingtimeout = 10000;
51 andrew.betts 7 this.status = 0;
52 andrew.betts 6
53 andrew.betts 7 /* Statuses: 0 = Uninitialised,
54     1 = Loading stream,
55     2 = Loading controller frame,
56 andrew.betts 14 3 = Controller frame timeout, retrying.
57 andrew.betts 7 4 = Controller frame loaded and ready
58     5 = Receiving data
59     */
60    
61 andrew.betts 6 this.instID = (typeof(instID) != "undefined") ? instID : 0;
62 andrew.betts 14 this.MHostId = Math.floor(Math.random()*100000000)+""+this.instID;
63 andrew.betts 6 }
64    
65     Meteor.instances = new Array();
66    
67     Meteor.create = function(instID) {
68 andrew.betts 9 if (!instID) instID = Meteor.instances.length;
69 andrew.betts 6 Meteor.instances[instID] = new Meteor(instID);
70     return Meteor.instances[instID];
71     }
72    
73     Meteor.register = function(ifr) {
74     instid = new String(ifr.window.frameElement.id);
75 andrew.betts 14 instid = instid.replace(/.*_([0-9]*)$/, "$1");
76 andrew.betts 6 ifr.p = this.instances[instid].process.bind(this.instances[instid]);
77     ifr.r = this.instances[instid].reset.bind(this.instances[instid]);
78     ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);
79     ifr.get = this.instances[instid].get.bind(this.instances[instid]);
80     ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);
81 andrew.betts 7 clearTimeout(this.instances[instid].frameloadtimer);
82     this.instances[instid].setstatus(4);
83 andrew.betts 14 if (this.debugmode) console.log("Frame registered");
84 andrew.betts 6 }
85    
86     Meteor.prototype.start = function() {
87     this.persist = (this.maxmessages)?1:0;
88     this.smartpoll = (this.smartpoll)?1:0;
89     this.mode = (this.mode=="stream")?"stream":"poll";
90     if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified";
91 andrew.betts 14 this.stop();
92 andrew.betts 6 var now = new Date();
93     var t = now.getTime();
94 andrew.betts 14 this.setstatus(1);
95 andrew.betts 6 if (this.mode=="stream") {
96 andrew.betts 14 var surl = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId;
97     if (this.lastmsgreceived >= 0) {
98     surl += "&restartfrom="+this.lastmsgreceived;
99     } else if (this.backtrack > 0) {
100     surl += "&backtrack="+this.backtrack;
101     } else if (this.backtrack < 0 || isNaN(this.backtrack)) {
102     surl += "&restartfrom=";
103 andrew.betts 6 }
104 andrew.betts 14 this.createIframe(surl);
105 andrew.betts 6 var f = this.pollmode.bind(this);
106     clearTimeout(this.pingtimer);
107     this.pingtimer = setTimeout(f, this.pingtimeout);
108    
109     } else {
110 andrew.betts 14 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 andrew.betts 6 var ifr = document.createElement("IFRAME");
133     ifr.style.width = "10px";
134     ifr.style.height = "10px";
135     ifr.style.border = "none";
136 andrew.betts 14 ifr.style.position = "absolute";
137     ifr.style.top = "-10px";
138     ifr.style.marginTop = "-10px";
139     ifr.style.zIndex = "-20";
140     ifr.setAttribute("id", "meteorframe_"+this.instID);
141     ifr.Meteor = Meteor;
142     var innerifr = document.createElement("IFRAME");
143     innerifr.setAttribute("src", url);
144     innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
145     ifr.appendChild(innerifr);
146 andrew.betts 6 document.body.appendChild(ifr);
147     }
148 andrew.betts 14 if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
149     var f = this.frameloadtimeout.bind(this);
150     this.frameloadtimer = setTimeout(f, 5000);
151 andrew.betts 6 }
152    
153 andrew.betts 9 Meteor.prototype.stop = function() {
154     if (typeof(this.transferDoc)=="object") {
155 andrew.betts 14 this.transferDoc = false;
156 andrew.betts 9 }
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 andrew.betts 6 Meteor.prototype.pollmode = function() {
168 andrew.betts 14 if (this.debugmode) console.log("Ping timeout");
169     this.stop();
170 andrew.betts 6 this.mode="poll";
171     this.start();
172     this.callback_changemode("poll");
173 andrew.betts 7 this.lastpingtime = false;
174 andrew.betts 6 }
175    
176 andrew.betts 14 Meteor.prototype.process = function(id, data, timestamp) {
177 andrew.betts 6 if (id > this.lastmsgreceived) {
178 andrew.betts 14 this.callback_process(data, timestamp);
179 andrew.betts 6 if (id != -1) this.lastmsgreceived = id;
180     if (this.mode=="poll") {
181     var now = new Date();
182     var t = now.getTime();
183     this.recvtimes[this.recvtimes.length] = t;
184     while (this.recvtimes.length > 5) this.recvtimes.shift();
185     }
186     } else if (id == -1) {
187     this.ping();
188     }
189 andrew.betts 7 this.setstatus(5);
190 andrew.betts 6 }
191    
192     Meteor.prototype.ping = function() {
193     if (this.mode=="stream" && this.pingtimer) {
194     clearTimeout(this.pingtimer);
195     var f = this.pollmode.bind(this);
196     this.pingtimer = setTimeout(f, this.pingtimeout);
197     var now = new Date();
198     this.lastpingtime = now.getTime();
199     }
200 andrew.betts 7 this.setstatus(5);
201 andrew.betts 6 }
202    
203     Meteor.prototype.reset = function() {
204 andrew.betts 14 if (this.debugmode) console.log("Stream reset");
205 andrew.betts 6 var now = new Date();
206     var t = now.getTime();
207     var x = this.pollfreq - (t-this.lastrequest);
208     if (x < 10) x = 10;
209     this.ping();
210     this.callback_reset();
211     setTimeout(this.start.bind(this), x);
212     }
213    
214     Meteor.prototype.eof = function() {
215     this.callback_eof();
216     }
217    
218     Meteor.prototype.get = function(varname) {
219     eval("var a = this."+varname+";");
220     if (typeof(a) == "undefined") throw "Cannot get value of "+varname;
221     return a;
222     }
223    
224     Meteor.prototype.increasepolldelay = function() {
225     this.pollfreq *= 2;
226     }
227    
228     Meteor.prototype.updatepollfreq = function() {
229     if (this.smartpoll) {
230     var now = new Date();
231     var t = now.getTime();
232     var avg = 0;
233     for (var i=1; i<this.recvtimes.length; i++) {
234     var x = (this.recvtimes[i]-this.recvtimes[i-1]);
235     avg += (x>60000)? 60000 : x;
236     }
237     x = (t-this.recvtimes[this.recvtimes.length-1]);
238     avg += (x>180000)? 180000 : x;
239     avg /= this.recvtimes.length;
240     if ((avg/3) < this.pollfreq && (avg/3) >= this.minpollfreq) this.pollfreq = Math.ceil(this.pollfreq*0.9);
241     if ((avg/3) > this.pollfreq) this.pollfreq = Math.floor(this.pollfreq*1.05);
242     }
243     }
244    
245     Meteor.prototype.registerEventCallback = function(evt, funcRef) {
246     if (evt=="process") {
247     this.callback_process = (this.callback_process).andThen(funcRef);
248     } else if (evt=="reset") {
249     this.callback_reset = (this.callback_reset).andThen(funcRef);
250     } else if (evt=="eof") {
251     this.callback_eof = (this.callback_eof).andThen(funcRef);
252     } else if (evt=="changemode") {
253     this.callback_changemode = (this.callback_changemode).andThen(funcRef);
254 andrew.betts 7 } else if (evt=="changestatus") {
255     this.callback_statuschanged = (this.callback_statuschanged).andThen(funcRef);
256 andrew.betts 6 }
257     }
258    
259 andrew.betts 7 Meteor.prototype.frameloadtimeout = function() {
260 andrew.betts 14 if (this.debugmode) console.log("Frame load timeout");
261 andrew.betts 7 if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
262     this.setstatus(3);
263 andrew.betts 14 setTimeout(this.start.bind(this), 5000);
264 andrew.betts 7 }
265     Meteor.prototype.setstatus = function(newstatus) {
266     if (this.status != newstatus) {
267     this.status = newstatus;
268     this.callback_statuschanged(newstatus);
269     }
270     }
271 andrew.betts 6
272     Meteor.createCookie = function(name,value,days) {
273     if (days) {
274     var date = new Date();
275     date.setTime(date.getTime()+(days*24*60*60*1000));
276     var expires = "; expires="+date.toGMTString();
277     }
278     else var expires = "";
279     document.cookie = name+"="+value+expires+"; path=/";
280     }
281    
282     Meteor.readCookie = function(name) {
283     var nameEQ = name + "=";
284     var ca = document.cookie.split(';');
285     for(var i=0;i < ca.length;i++) {
286     var c = ca[i];
287     while (c.charAt(0)==' ') c = c.substring(1,c.length);
288     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
289     }
290     return null;
291     }
292    
293     Meteor.eraseCookie = function(name) {
294     createCookie(name,"",-1);
295 andrew.betts 3 }

  ViewVC Help
Powered by ViewVC 1.1.26