/[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 18 - (hide annotations)
Wed May 2 10:55:26 2007 UTC (17 years ago) by andrew.betts
File MIME type: application/javascript
File size: 10313 byte(s)
updated javascript client

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

  ViewVC Help
Powered by ViewVC 1.1.26