/[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 64 - (hide annotations)
Mon Jan 19 11:19:41 2009 UTC (15 years, 3 months ago) by andrew.betts
File MIME type: application/javascript
File size: 11158 byte(s)
Release 1.06.04 as documented in Google Group

1 andrew.betts 32 /*
2     stream: xhrinteractive, iframe, serversent
3     longpoll
4     smartpoll
5     simplepoll
6     */
7    
8     Meteor = {
9    
10     callbacks: {
11     process: function() {},
12     reset: function() {},
13     eof: function() {},
14     statuschanged: function() {},
15     changemode: function() {}
16     },
17     channelcount: 0,
18     channels: {},
19     debugmode: false,
20     frameref: null,
21     host: null,
22     hostid: null,
23     maxpollfreq: 60000,
24     minpollfreq: 2000,
25     mode: "stream",
26     pingtimeout: 20000,
27     pingtimer: null,
28 andrew.betts 39 pollfreq: 3000,
29 andrew.betts 32 port: 80,
30 andrew.betts 62 pollaborted: false,
31     pollhost: null,
32     pollnum: 0,
33 andrew.betts 32 polltimeout: 30000,
34 andrew.betts 62 polltimer: null,
35 andrew.betts 32 recvtimes: [],
36 andrew.betts 62 lastrequest: null,
37 andrew.betts 32 status: 0,
38     updatepollfreqtimer: null,
39    
40 andrew.betts 62 isSupportedBrowser: function() {
41     var v;
42     if (v = navigator.userAgent.match(/compatible\; MSIE\ ([0-9\.]+)\;/i)) {
43     if (parseFloat(v[1]) <= 5.5) return false;
44     } else if (v = navigator.userAgent.match(/Gecko\/([0-9]+)/i)) {
45     if (parseInt(v[1]) <= 20051015) return false;
46     } else if (v = navigator.userAgent.match(/WebKit\/([0-9\.]+)/i)) {
47     if (parseFloat(v[1]) < 400) return false;
48     }
49     return true;
50     },
51    
52 andrew.betts 32 register: function(ifr) {
53     ifr.p = Meteor.process;
54     ifr.r = Meteor.reset;
55     ifr.eof = Meteor.eof;
56 andrew.betts 53 ifr.ch = Meteor.channelInfo;
57 andrew.betts 32 clearTimeout(Meteor.frameloadtimer);
58     Meteor.setstatus(4);
59     Meteor.log("Frame registered");
60     },
61    
62     joinChannel: function(channelname, backtrack) {
63     if (typeof(Meteor.channels[channelname]) != "undefined") throw "Cannot join channel "+channelname+": already subscribed";
64 andrew.betts 59 Meteor.channels[channelname] = {backtrack:backtrack};
65 andrew.betts 32 Meteor.log("Joined channel "+channelname);
66     Meteor.channelcount++;
67 andrew.betts 61 if (Meteor.status != 0 && Meteor.status != 6) Meteor.connect();
68 andrew.betts 32 },
69    
70     leaveChannel: function(channelname) {
71     if (typeof(Meteor.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed";
72     delete Meteor.channels[channelname];
73     Meteor.log("Left channel "+channelname);
74     Meteor.channelcount--;
75 andrew.betts 61 if (Meteor.channelcount && Meteor.status != 0 && Meteor.status != 6) Meteor.connect();
76 andrew.betts 59 else Meteor.disconnect();
77 andrew.betts 32 },
78    
79     connect: function() {
80     if (!Meteor.host) throw "Meteor host not specified";
81     if (isNaN(Meteor.port)) throw "Meteor port not specified";
82     if (!Meteor.channelcount) throw "No channels specified";
83     if (Meteor.status) Meteor.disconnect();
84 andrew.betts 64 Meteor.log("Connecting");
85 andrew.betts 32 Meteor.setstatus(1);
86 andrew.betts 62 if (!Meteor.hostid) Meteor.hostid = Meteor.time()+""+Math.floor(Math.random()*1000000)
87 andrew.betts 32 document.domain = Meteor.extract_xss_domain(document.domain);
88     if (Meteor.mode=="stream") Meteor.mode = Meteor.selectStreamTransport();
89     Meteor.log("Selected "+Meteor.mode+" transport");
90     if (Meteor.mode=="xhrinteractive" || Meteor.mode=="iframe" || Meteor.mode=="serversent") {
91     if (Meteor.mode == "iframe") {
92     Meteor.loadFrame(Meteor.getSubsUrl());
93     } else {
94     Meteor.loadFrame("http://"+Meteor.host+((Meteor.port==80)?"":":"+Meteor.port)+"/stream.html");
95     }
96     clearTimeout(Meteor.pingtimer);
97     Meteor.pingtimer = setTimeout(Meteor.pollmode, Meteor.pingtimeout);
98    
99     } else {
100 andrew.betts 62 Meteor.recvtimes[0] = Meteor.time();
101 andrew.betts 32 if (Meteor.updatepollfreqtimer) clearTimeout(Meteor.updatepollfreqtimer);
102 andrew.betts 60 if (Meteor.mode=='smartpoll') Meteor.updatepollfreqtimer = setInterval(Meteor.updatepollfreq, 10000);
103 andrew.betts 32 if (Meteor.mode=='longpoll') Meteor.pollfreq = Meteor.minpollfreq;
104 andrew.betts 62 Meteor.poll();
105 andrew.betts 32 }
106     },
107    
108     disconnect: function() {
109     if (Meteor.status) {
110 andrew.betts 64 if (Meteor.status != 6) Meteor.setstatus(0);
111     Meteor.clearpoll();
112 andrew.betts 32 clearTimeout(Meteor.pingtimer);
113     clearTimeout(Meteor.updatepollfreqtimer);
114     clearTimeout(Meteor.frameloadtimer);
115     if (typeof CollectGarbage == 'function') CollectGarbage();
116 andrew.betts 55 Meteor.log("Disconnected");
117 andrew.betts 61 try { Meteor.frameref.parentNode.removeChild(Meteor.frameref); delete Meteor.frameref; return true; } catch(e) { }
118     try { Meteor.frameref.open(); Meteor.frameref.close(); return true; } catch(e) {}
119 andrew.betts 32 }
120     },
121    
122     selectStreamTransport: function() {
123     try {
124     var test = ActiveXObject;
125     return "iframe";
126     } catch (e) {}
127     if ((typeof window.addEventStream) == "function") return "iframe";
128     return "xhrinteractive";
129     },
130    
131     getSubsUrl: function() {
132 andrew.betts 62 var host = ((Meteor.mode=='simplepoll' || Meteor.mode=='smartpoll' || Meteor.mode=='longpoll') && Meteor.pollhost) ? Meteor.pollhost : Meteor.host;
133     var surl = "http://" + host + ((Meteor.port==80)?"":":"+Meteor.port) + "/push/" + Meteor.hostid + "/" + Meteor.mode;
134 andrew.betts 32 for (var c in Meteor.channels) {
135     surl += "/"+c;
136 andrew.betts 62 if (typeof Meteor.channels[c].lastmsgreceived != 'undefined') {
137 andrew.betts 32 surl += ".r"+(Meteor.channels[c].lastmsgreceived+1);
138     } else if (Meteor.channels[c].backtrack > 0) {
139     surl += ".b"+Meteor.channels[c].backtrack;
140 andrew.betts 59 } else if (Meteor.channels[c].backtrack != undefined) {
141 andrew.betts 32 surl += ".h";
142     }
143     }
144 andrew.betts 62 surl += "?nc="+Meteor.time();
145 andrew.betts 32 return surl;
146     },
147    
148     loadFrame: function(url) {
149     try {
150 andrew.betts 39 if (!Meteor.frameref) {
151     var transferDoc = new ActiveXObject("htmlfile");
152     Meteor.frameref = transferDoc;
153     }
154     Meteor.frameref.open();
155     Meteor.frameref.write("<html><script>");
156     Meteor.frameref.write("document.domain=\""+(document.domain)+"\";");
157     Meteor.frameref.write("</"+"script></html>");
158     Meteor.frameref.parentWindow.Meteor = Meteor;
159     Meteor.frameref.close();
160     var ifrDiv = Meteor.frameref.createElement("div");
161     Meteor.frameref.appendChild(ifrDiv);
162 andrew.betts 32 ifrDiv.innerHTML = "<iframe src=\""+url+"\"></iframe>";
163     } catch (e) {
164 andrew.betts 39 if (!Meteor.frameref) {
165     var ifr = document.createElement("IFRAME");
166     ifr.style.width = "10px";
167     ifr.style.height = "10px";
168     ifr.style.border = "none";
169     ifr.style.position = "absolute";
170     ifr.style.top = "-10px";
171     ifr.style.marginTop = "-10px";
172     ifr.style.zIndex = "-20";
173     ifr.Meteor = Meteor;
174     document.body.appendChild(ifr);
175     Meteor.frameref = ifr;
176     }
177     Meteor.frameref.setAttribute("src", url);
178 andrew.betts 32 }
179     Meteor.log("Loading URL '"+url+"' into frame...");
180     Meteor.frameloadtimer = setTimeout(Meteor.frameloadtimeout, 5000);
181     },
182    
183     pollmode: function() {
184     Meteor.log("Ping timeout");
185 andrew.betts 60 if (Meteor.mode != "smartpoll") {
186     Meteor.mode="smartpoll";
187     Meteor.callbacks["changemode"]("poll");
188     clearTimeout(Meteor.pingtimer);
189     Meteor.lastpingtime = false;
190     }
191 andrew.betts 53 Meteor.connect();
192 andrew.betts 32 },
193    
194     process: function(id, channel, data) {
195     if (id == -1) {
196     Meteor.log("Ping");
197     Meteor.ping();
198 andrew.betts 53 } else if (typeof(Meteor.channels[channel]) != "undefined") {
199 andrew.betts 32 Meteor.log("Message "+id+" received on channel "+channel+" (last id on channel: "+Meteor.channels[channel].lastmsgreceived+")\n"+data);
200     Meteor.callbacks["process"](data);
201     Meteor.channels[channel].lastmsgreceived = id;
202     if (Meteor.mode=="smartpoll") {
203 andrew.betts 62 Meteor.recvtimes[Meteor.recvtimes.length] = Meteor.time();
204 andrew.betts 32 while (Meteor.recvtimes.length > 5) Meteor.recvtimes.shift();
205     }
206     }
207     Meteor.setstatus(5);
208     },
209    
210     ping: function() {
211     if (Meteor.pingtimer) {
212     clearTimeout(Meteor.pingtimer);
213     Meteor.pingtimer = setTimeout(Meteor.pollmode, Meteor.pingtimeout);
214 andrew.betts 62 Meteor.lastpingtime = Meteor.time();
215 andrew.betts 32 }
216     Meteor.setstatus(5);
217     },
218    
219     reset: function() {
220 andrew.betts 61 if (Meteor.status != 6 && Meteor.status != 0) {
221 andrew.betts 55 Meteor.log("Stream reset");
222     Meteor.ping();
223     Meteor.callbacks["reset"]();
224 andrew.betts 62 var x = Meteor.pollfreq - (Meteor.time()-Meteor.lastrequest);
225 andrew.betts 55 if (x < 10) x = 10;
226     setTimeout(Meteor.connect, x);
227     }
228 andrew.betts 32 },
229    
230     eof: function() {
231 andrew.betts 55 Meteor.log("Received end of stream, will not reconnect");
232 andrew.betts 32 Meteor.callbacks["eof"]();
233 andrew.betts 55 Meteor.setstatus(6);
234 andrew.betts 53 Meteor.disconnect();
235 andrew.betts 32 },
236    
237 andrew.betts 53 channelInfo: function(channel, id) {
238     Meteor.channels[channel].lastmsgreceived = id;
239     Meteor.log("Received channel info for channel "+channel+": resume from "+id);
240     },
241    
242 andrew.betts 32 updatepollfreq: function() {
243     var avg = 0;
244     for (var i=1; i<Meteor.recvtimes.length; i++) {
245     avg += (Meteor.recvtimes[i]-Meteor.recvtimes[i-1]);
246     }
247 andrew.betts 62 avg += (Meteor.time()-Meteor.recvtimes[Meteor.recvtimes.length-1]);
248 andrew.betts 32 avg /= Meteor.recvtimes.length;
249     var target = avg/2;
250     if (target < Meteor.pollfreq && Meteor.pollfreq > Meteor.minpollfreq) Meteor.pollfreq = Math.ceil(Meteor.pollfreq*0.9);
251     if (target > Meteor.pollfreq && Meteor.pollfreq < Meteor.maxpollfreq) Meteor.pollfreq = Math.floor(Meteor.pollfreq*1.05);
252     },
253    
254     registerEventCallback: function(evt, funcRef) {
255     Function.prototype.andThen=function(g) {
256     var f=this;
257     var a=Meteor.arguments
258     return function(args) {
259     f(a);g(args);
260     }
261     };
262     if (typeof Meteor.callbacks[evt] == "function") {
263     Meteor.callbacks[evt] = (Meteor.callbacks[evt]).andThen(funcRef);
264     } else {
265     Meteor.callbacks[evt] = funcRef;
266     }
267     },
268    
269     frameloadtimeout: function() {
270     Meteor.log("Frame load timeout");
271     if (Meteor.frameloadtimer) clearTimeout(Meteor.frameloadtimer);
272     Meteor.setstatus(3);
273 andrew.betts 53 Meteor.pollmode();
274 andrew.betts 32 },
275    
276     extract_xss_domain: function(old_domain) {
277     if (old_domain.match(/^(\d{1,3}\.){3}\d{1,3}$/)) return old_domain;
278     domain_pieces = old_domain.split('.');
279     return domain_pieces.slice(-2, domain_pieces.length).join(".");
280     },
281    
282     setstatus: function(newstatus) {
283     // Statuses: 0 = Uninitialised,
284     // 1 = Loading stream,
285     // 2 = Loading controller frame,
286     // 3 = Controller frame timeout, retrying.
287     // 4 = Controller frame loaded and ready
288     // 5 = Receiving data
289 andrew.betts 55 // 6 = End of stream, will not reconnect
290 andrew.betts 32
291     if (Meteor.status != newstatus) {
292     Meteor.status = newstatus;
293     Meteor.callbacks["statuschanged"](newstatus);
294     }
295     },
296    
297     log: function(logstr) {
298     if (Meteor.debugmode) {
299     if (window.console) {
300     window.console.log(logstr);
301     } else if (document.getElementById("meteorlogoutput")) {
302     document.getElementById("meteorlogoutput").innerHTML += logstr+"<br/>";
303     }
304     }
305 andrew.betts 62 },
306    
307     poll: function() {
308     Meteor.pollaborted = 0;
309     try {
310     clearTimeout(Meteor.polltimer);
311     } catch (e) {};
312     Meteor.lastrequest = Meteor.time();
313     if (Meteor.polltimeout) Meteor.polltimer = setTimeout(Meteor.clearpoll, Meteor.polltimeout);
314     var scripttag = document.createElement("SCRIPT");
315     scripttag.type = "text/javascript";
316     scripttag.src = Meteor.getSubsUrl();
317     scripttag.id = "meteorpoll"+(++Meteor.pollnum);
318     scripttag.className = "meteorpoll";
319     document.getElementsByTagName("HEAD")[0].appendChild(scripttag);
320     },
321    
322     clearpoll: function() {
323 andrew.betts 64 if (document.getElementById('meteorpoll'+Meteor.pollnum)) {
324     var s = document.getElementById('meteorpoll'+Meteor.pollnum);
325     s.parentNode.removeChild(s);
326     }
327     if (Meteor.status == 5) {
328 andrew.betts 62 var x = parent.Meteor.pollfreq - (Meteor.time()-Meteor.lastrequest);
329     if (x < 10) x = 10;
330     setTimeout(Meteor.poll, x);
331     }
332     },
333    
334     time: function() {
335     var now = new Date();
336     return now.getTime();
337 andrew.betts 32 }
338     }
339    
340     var oldonunload = window.onunload;
341     if (typeof window.onunload != 'function') {
342     window.onunload = Meteor.disconnect;
343     } else {
344     window.onunload = function() {
345     if (oldonunload) oldonunload();
346     Meteor.disconnect();
347     }
348     }

  ViewVC Help
Powered by ViewVC 1.1.26