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