/[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 32 - (show annotations)
Thu Dec 20 21:24:24 2007 UTC (16 years, 4 months ago) by andrew.betts
File MIME type: application/javascript
File size: 8998 byte(s)
Complete rewrite of the JS web client
Opera and Safari compatibility
Saner request format

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

  ViewVC Help
Powered by ViewVC 1.1.26