/[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 20 - (hide annotations)
Sat May 5 15:04:41 2007 UTC (16 years, 11 months ago) by andrew.betts
File MIME type: application/javascript
File size: 10526 byte(s)
Updated defaults and docs, removed redundant line in meteor.js

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.transferDoc = false;
23     this.pingtimer = false;
24     this.updatepollfreqtimer = false;
25     this.lastrequest = 0;
26 andrew.betts 19 this.recvtimes = [];
27 andrew.betts 6 this.MHostId = false;
28     this.callback_process = function() {};
29     this.callback_reset = function() {};
30     this.callback_eof = function() {};
31     this.callback_changemode = function() {};
32 andrew.betts 7 this.callback_statuschanged = function() {};
33 andrew.betts 6 this.persist = true;
34 andrew.betts 7 this.frameloadtimer = false;
35 andrew.betts 14 this.debugmode = false;
36 andrew.betts 18 this.subsurl = false;
37 andrew.betts 19 this.channels = {};
38 andrew.betts 6
39     // Documented public properties
40     this.subdomain = "data";
41     this.dynamicpageaddress = "push";
42     this.smartpoll = true;
43     this.pollfreq = 2000;
44     this.minpollfreq = 2000;
45 andrew.betts 14 this.mode = "poll";
46 andrew.betts 6 this.polltimeout=30000;
47     this.pingtimeout = 10000;
48 andrew.betts 18 this.maxmessages = 0;
49 andrew.betts 7 this.status = 0;
50 andrew.betts 6
51 andrew.betts 7 /* Statuses: 0 = Uninitialised,
52     1 = Loading stream,
53     2 = Loading controller frame,
54 andrew.betts 14 3 = Controller frame timeout, retrying.
55 andrew.betts 7 4 = Controller frame loaded and ready
56     5 = Receiving data
57     */
58    
59 andrew.betts 6 this.instID = (typeof(instID) != "undefined") ? instID : 0;
60 andrew.betts 14 this.MHostId = Math.floor(Math.random()*100000000)+""+this.instID;
61 andrew.betts 6 }
62    
63     Meteor.instances = new Array();
64    
65     Meteor.create = function(instID) {
66 andrew.betts 9 if (!instID) instID = Meteor.instances.length;
67 andrew.betts 6 Meteor.instances[instID] = new Meteor(instID);
68     return Meteor.instances[instID];
69     }
70    
71     Meteor.register = function(ifr) {
72     instid = new String(ifr.window.frameElement.id);
73 andrew.betts 14 instid = instid.replace(/.*_([0-9]*)$/, "$1");
74 andrew.betts 6 ifr.p = this.instances[instid].process.bind(this.instances[instid]);
75     ifr.r = this.instances[instid].reset.bind(this.instances[instid]);
76     ifr.eof = this.instances[instid].eof.bind(this.instances[instid]);
77     ifr.get = this.instances[instid].get.bind(this.instances[instid]);
78     ifr.increasepolldelay = this.instances[instid].increasepolldelay.bind(this.instances[instid]);
79 andrew.betts 7 clearTimeout(this.instances[instid].frameloadtimer);
80     this.instances[instid].setstatus(4);
81 andrew.betts 14 if (this.debugmode) console.log("Frame registered");
82 andrew.betts 6 }
83    
84 andrew.betts 18 Meteor.reset = function(ifr) {
85     instid = new String(ifr.window.frameElement.id);
86     instid = instid.replace(/.*_([0-9]*)$/, "$1");
87     this.instances[instid].reset();
88     }
89    
90     Meteor.prototype.joinChannel = function(channelname, backtrack) {
91     if (typeof(this.channels[channelname]) != "undefined") throw "Cannot join channel "+channelname+": already subscribed";
92     this.channels[channelname] = {backtrack:backtrack, lastmsgreceived:0};
93 andrew.betts 19 if (this.debugmode) console.log("Joined channel "+channelname+", channel list follows");
94     if (this.debugmode) console.log(this.channels);
95 andrew.betts 18 if (this.status != 0) this.start();
96     }
97    
98     Meteor.prototype.leaveChannel = function(channelname) {
99     if (typeof(this.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed";
100     delete this.channels[channelname];
101     if (this.status != 0) this.start();
102     }
103    
104 andrew.betts 6 Meteor.prototype.start = function() {
105     this.persist = (this.maxmessages)?1:0;
106     this.smartpoll = (this.smartpoll)?1:0;
107     this.mode = (this.mode=="stream")?"stream":"poll";
108 andrew.betts 18 if (!this.subdomain || this.channels.length) throw "Channel or Meteor subdomain host not specified";
109 andrew.betts 14 this.stop();
110 andrew.betts 6 var now = new Date();
111     var t = now.getTime();
112 andrew.betts 14 this.setstatus(1);
113 andrew.betts 18 var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId;
114     if (this.maxmessages && !this.persist) surl += "&maxmessages=" + this.maxmessages;
115     for (var c in this.channels) {
116     surl += "&channel="+c;
117     if (this.channels[c].lastmsgreceived >= 0) {
118     surl += "&restartfrom="+this.channels[c].lastmsgreceived;
119     } else if (this.channels[c].backtrack > 0) {
120     surl += "&backtrack="+this.channels[c].backtrack;
121     } else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) {
122 andrew.betts 14 surl += "&restartfrom=";
123 andrew.betts 6 }
124 andrew.betts 18 }
125     this.subsurl = surl;
126     if (this.mode=="stream") {
127     this.createIframe(this.subsurl);
128 andrew.betts 6 var f = this.pollmode.bind(this);
129     clearTimeout(this.pingtimer);
130     this.pingtimer = setTimeout(f, this.pingtimeout);
131    
132     } else {
133 andrew.betts 14 this.createIframe("http://"+this.subdomain+"."+location.hostname+"/poll.html");
134     this.recvtimes[0] = t;
135     if (this.updatepollfreqtimer) clearTimeout(this.updatepollfreqtimer);
136     this.updatepollfreqtimer = setInterval(this.updatepollfreq.bind(this), 2500);
137     }
138     this.lastrequest = t;
139     }
140    
141     Meteor.prototype.createIframe = function(url) {
142     if (document.all) {
143     this.transferDoc = new ActiveXObject("htmlfile");
144     this.transferDoc.open();
145     this.transferDoc.write("<html>");
146     this.transferDoc.write("<script>document.domain=\""+(document.domain)+"\";</"+"script>");
147     this.transferDoc.write("</html>");
148     this.transferDoc.parentWindow.Meteor = Meteor;
149     this.transferDoc.close();
150     var ifrDiv = this.transferDoc.createElement("div");
151     this.transferDoc.appendChild(ifrDiv);
152     ifrDiv.innerHTML = "<iframe id=\"meteorframe_"+this.instID+"\" src=\""+url+"\" style=\"display: none;\"></iframe>";
153     } else {
154 andrew.betts 6 var ifr = document.createElement("IFRAME");
155     ifr.style.width = "10px";
156     ifr.style.height = "10px";
157     ifr.style.border = "none";
158 andrew.betts 14 ifr.style.position = "absolute";
159     ifr.style.top = "-10px";
160     ifr.style.marginTop = "-10px";
161     ifr.style.zIndex = "-20";
162     ifr.setAttribute("id", "meteorframe_"+this.instID);
163     ifr.Meteor = Meteor;
164     var innerifr = document.createElement("IFRAME");
165     innerifr.setAttribute("src", url);
166     innerifr.setAttribute("id", "meteorinnerframe_"+this.instID);
167     ifr.appendChild(innerifr);
168 andrew.betts 6 document.body.appendChild(ifr);
169     }
170 andrew.betts 14 if (this.debugmode) console.log("Loading URL '"+url+"' into frame...");
171     var f = this.frameloadtimeout.bind(this);
172     this.frameloadtimer = setTimeout(f, 5000);
173 andrew.betts 6 }
174    
175 andrew.betts 9 Meteor.prototype.stop = function() {
176     if (typeof(this.transferDoc)=="object") {
177 andrew.betts 14 this.transferDoc = false;
178 andrew.betts 9 }
179     if (document.getElementById("meteorframe_"+this.instID)) {
180     document.getElementById("meteorframe_"+this.instID).src="about:blank";
181     document.body.removeChild(document.getElementById("meteorframe_"+this.instID));
182     }
183     if (!isNaN(this.pingtimer)) clearTimeout(this.pingtimer);
184     if (!isNaN(this.updatepollfreqtimer)) clearTimeout(this.updatepollfreqtimer);
185     if (!isNaN(this.frameloadtimer)) clearTimeout(this.frameloadtimer);
186     this.setstatus(0);
187     }
188    
189 andrew.betts 6 Meteor.prototype.pollmode = function() {
190 andrew.betts 14 if (this.debugmode) console.log("Ping timeout");
191 andrew.betts 6 this.mode="poll";
192     this.start();
193     this.callback_changemode("poll");
194 andrew.betts 7 this.lastpingtime = false;
195 andrew.betts 6 }
196    
197 andrew.betts 18 Meteor.prototype.process = function(id, channel, data) {
198 andrew.betts 19 if (id == -1) {
199     if (this.debugmode) console.log("Ping");
200     this.ping();
201     } else if (typeof(this.channels[channel]) != "undefined" && id > this.channels[channel].lastmsgreceived) {
202     if (this.debugmode) console.log("Message "+id+" received on channel "+channel+" (last id on channel: "+this.channels[channel].lastmsgreceived+")\n"+data);
203 andrew.betts 18 this.callback_process(data);
204     this.channels[channel].lastmsgreceived = id;
205 andrew.betts 6 if (this.mode=="poll") {
206     var now = new Date();
207     var t = now.getTime();
208     this.recvtimes[this.recvtimes.length] = t;
209     while (this.recvtimes.length > 5) this.recvtimes.shift();
210     }
211     }
212 andrew.betts 7 this.setstatus(5);
213 andrew.betts 6 }
214    
215     Meteor.prototype.ping = function() {
216     if (this.mode=="stream" && this.pingtimer) {
217     clearTimeout(this.pingtimer);
218     var f = this.pollmode.bind(this);
219     this.pingtimer = setTimeout(f, this.pingtimeout);
220     var now = new Date();
221     this.lastpingtime = now.getTime();
222     }
223 andrew.betts 7 this.setstatus(5);
224 andrew.betts 6 }
225    
226     Meteor.prototype.reset = function() {
227 andrew.betts 14 if (this.debugmode) console.log("Stream reset");
228 andrew.betts 6 var now = new Date();
229     var t = now.getTime();
230     var x = this.pollfreq - (t-this.lastrequest);
231     if (x < 10) x = 10;
232     this.ping();
233     this.callback_reset();
234     setTimeout(this.start.bind(this), x);
235     }
236    
237     Meteor.prototype.eof = function() {
238     this.callback_eof();
239     }
240    
241     Meteor.prototype.get = function(varname) {
242     eval("var a = this."+varname+";");
243     if (typeof(a) == "undefined") throw "Cannot get value of "+varname;
244     return a;
245     }
246    
247     Meteor.prototype.increasepolldelay = function() {
248     this.pollfreq *= 2;
249     }
250    
251     Meteor.prototype.updatepollfreq = function() {
252     if (this.smartpoll) {
253     var now = new Date();
254     var t = now.getTime();
255     var avg = 0;
256     for (var i=1; i<this.recvtimes.length; i++) {
257     var x = (this.recvtimes[i]-this.recvtimes[i-1]);
258     avg += (x>60000)? 60000 : x;
259     }
260     x = (t-this.recvtimes[this.recvtimes.length-1]);
261     avg += (x>180000)? 180000 : x;
262     avg /= this.recvtimes.length;
263     if ((avg/3) < this.pollfreq && (avg/3) >= this.minpollfreq) this.pollfreq = Math.ceil(this.pollfreq*0.9);
264     if ((avg/3) > this.pollfreq) this.pollfreq = Math.floor(this.pollfreq*1.05);
265     }
266     }
267    
268     Meteor.prototype.registerEventCallback = function(evt, funcRef) {
269     if (evt=="process") {
270     this.callback_process = (this.callback_process).andThen(funcRef);
271     } else if (evt=="reset") {
272     this.callback_reset = (this.callback_reset).andThen(funcRef);
273     } else if (evt=="eof") {
274     this.callback_eof = (this.callback_eof).andThen(funcRef);
275     } else if (evt=="changemode") {
276     this.callback_changemode = (this.callback_changemode).andThen(funcRef);
277 andrew.betts 7 } else if (evt=="changestatus") {
278     this.callback_statuschanged = (this.callback_statuschanged).andThen(funcRef);
279 andrew.betts 6 }
280     }
281    
282 andrew.betts 7 Meteor.prototype.frameloadtimeout = function() {
283 andrew.betts 14 if (this.debugmode) console.log("Frame load timeout");
284 andrew.betts 7 if (this.frameloadtimer) clearTimeout(this.frameloadtimer);
285     this.setstatus(3);
286 andrew.betts 14 setTimeout(this.start.bind(this), 5000);
287 andrew.betts 7 }
288     Meteor.prototype.setstatus = function(newstatus) {
289     if (this.status != newstatus) {
290     this.status = newstatus;
291     this.callback_statuschanged(newstatus);
292     }
293     }
294 andrew.betts 6
295     Meteor.createCookie = function(name,value,days) {
296     if (days) {
297     var date = new Date();
298     date.setTime(date.getTime()+(days*24*60*60*1000));
299     var expires = "; expires="+date.toGMTString();
300     }
301     else var expires = "";
302     document.cookie = name+"="+value+expires+"; path=/";
303     }
304    
305     Meteor.readCookie = function(name) {
306     var nameEQ = name + "=";
307     var ca = document.cookie.split(';');
308     for(var i=0;i < ca.length;i++) {
309     var c = ca[i];
310     while (c.charAt(0)==' ') c = c.substring(1,c.length);
311     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
312     }
313     return null;
314     }
315    
316     Meteor.eraseCookie = function(name) {
317     createCookie(name,"",-1);
318 andrew.betts 3 }

  ViewVC Help
Powered by ViewVC 1.1.26