/[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 59 - (show annotations)
Tue Mar 25 00:38:17 2008 UTC (16 years ago) by andrew.betts
File MIME type: application/javascript
File size: 9677 byte(s)
Fixed leaveChannel bug

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

  ViewVC Help
Powered by ViewVC 1.1.26