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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Thu Nov 23 15:53:25 2006 UTC (17 years, 4 months ago) by andrew.betts
File MIME type: application/javascript
File size: 10248 byte(s)
updated to add more robust frame loading code

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

  ViewVC Help
Powered by ViewVC 1.1.26