/[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 55 - (hide annotations)
Thu Feb 28 00:21:39 2008 UTC (16 years, 2 months ago) by andrew.betts
File MIME type: application/javascript
File size: 9430 byte(s)
Fixed: Made eof() work properly
Added nocache querystring param and modified Subscriber.pm to allow it
Rewrote stream.html to ensure we capture the last chunk of data before a reset
Incremented version

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

  ViewVC Help
Powered by ViewVC 1.1.26