/[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 9 - (hide annotations)
Fri Dec 8 16:52:58 2006 UTC (17 years, 4 months ago) by andrew.betts
File MIME type: application/javascript
File size: 10619 byte(s)


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

  ViewVC Help
Powered by ViewVC 1.1.26