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