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 |
andrew.betts |
59 |
Meteor.channels[channelname] = {backtrack:backtrack}; |
48 |
andrew.betts |
32 |
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 |
andrew.betts |
59 |
if (Meteor.channelcount && Meteor.status != 0) Meteor.connect(); |
59 |
|
|
else Meteor.disconnect(); |
60 |
andrew.betts |
32 |
}, |
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 |
andrew.betts |
60 |
if (Meteor.mode=='smartpoll') Meteor.updatepollfreqtimer = setInterval(Meteor.updatepollfreq, 10000); |
89 |
andrew.betts |
32 |
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 |
andrew.betts |
55 |
if (Meteor.status != 6) Meteor.setstatus(0); |
101 |
andrew.betts |
60 |
if (Meteor.frameref.tagName=='IFRAME') { |
102 |
|
|
Meteor.frameref.parentNode.removeChild(Meteor.frameref); |
103 |
|
|
} else { |
104 |
andrew.betts |
59 |
Meteor.frameref.open(); |
105 |
|
|
Meteor.frameref.close(); |
106 |
|
|
} |
107 |
|
|
delete Meteor.frameref; |
108 |
andrew.betts |
55 |
Meteor.log("Disconnected"); |
109 |
andrew.betts |
32 |
} |
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 |
andrew.betts |
59 |
if (typeof Meteor.channels[c].lastmsgreceived != 'undefined' && Meteor.channels[c].lastmsgreceived >= 0) { |
126 |
andrew.betts |
32 |
surl += ".r"+(Meteor.channels[c].lastmsgreceived+1); |
127 |
|
|
} else if (Meteor.channels[c].backtrack > 0) { |
128 |
|
|
surl += ".b"+Meteor.channels[c].backtrack; |
129 |
andrew.betts |
59 |
} else if (Meteor.channels[c].backtrack != undefined) { |
130 |
andrew.betts |
32 |
surl += ".h"; |
131 |
|
|
} |
132 |
|
|
} |
133 |
andrew.betts |
55 |
var now = new Date(); |
134 |
|
|
surl += "?nc="+now.getTime(); |
135 |
andrew.betts |
32 |
return surl; |
136 |
|
|
}, |
137 |
|
|
|
138 |
|
|
loadFrame: function(url) { |
139 |
|
|
try { |
140 |
andrew.betts |
39 |
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 |
andrew.betts |
32 |
ifrDiv.innerHTML = "<iframe src=\""+url+"\"></iframe>"; |
153 |
|
|
} catch (e) { |
154 |
andrew.betts |
39 |
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 |
andrew.betts |
32 |
} |
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 |
andrew.betts |
60 |
if (Meteor.mode != "smartpoll") { |
176 |
|
|
Meteor.mode="smartpoll"; |
177 |
|
|
Meteor.callbacks["changemode"]("poll"); |
178 |
|
|
clearTimeout(Meteor.pingtimer); |
179 |
|
|
Meteor.lastpingtime = false; |
180 |
|
|
} |
181 |
andrew.betts |
53 |
Meteor.connect(); |
182 |
andrew.betts |
32 |
}, |
183 |
|
|
|
184 |
|
|
process: function(id, channel, data) { |
185 |
|
|
if (id == -1) { |
186 |
|
|
Meteor.log("Ping"); |
187 |
|
|
Meteor.ping(); |
188 |
andrew.betts |
53 |
} else if (typeof(Meteor.channels[channel]) != "undefined") { |
189 |
andrew.betts |
32 |
Meteor.log("Message "+id+" received on channel "+channel+" (last id on channel: "+Meteor.channels[channel].lastmsgreceived+")\n"+data); |
190 |
|
|
Meteor.callbacks["process"](data); |
191 |
|
|
Meteor.channels[channel].lastmsgreceived = id; |
192 |
|
|
if (Meteor.mode=="smartpoll") { |
193 |
|
|
var now = new Date(); |
194 |
|
|
Meteor.recvtimes[Meteor.recvtimes.length] = now.getTime(); |
195 |
|
|
while (Meteor.recvtimes.length > 5) Meteor.recvtimes.shift(); |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
Meteor.setstatus(5); |
199 |
|
|
}, |
200 |
|
|
|
201 |
|
|
ping: function() { |
202 |
|
|
if (Meteor.pingtimer) { |
203 |
|
|
clearTimeout(Meteor.pingtimer); |
204 |
|
|
Meteor.pingtimer = setTimeout(Meteor.pollmode, Meteor.pingtimeout); |
205 |
|
|
var now = new Date(); |
206 |
|
|
Meteor.lastpingtime = now.getTime(); |
207 |
|
|
} |
208 |
|
|
Meteor.setstatus(5); |
209 |
|
|
}, |
210 |
|
|
|
211 |
|
|
reset: function() { |
212 |
andrew.betts |
55 |
if (Meteor.status != 6) { |
213 |
|
|
Meteor.log("Stream reset"); |
214 |
|
|
Meteor.ping(); |
215 |
|
|
Meteor.callbacks["reset"](); |
216 |
|
|
var now = new Date(); |
217 |
|
|
var t = now.getTime(); |
218 |
|
|
var x = Meteor.pollfreq - (t-Meteor.lastrequest); |
219 |
|
|
if (x < 10) x = 10; |
220 |
|
|
setTimeout(Meteor.connect, x); |
221 |
|
|
} |
222 |
andrew.betts |
32 |
}, |
223 |
|
|
|
224 |
|
|
eof: function() { |
225 |
andrew.betts |
55 |
Meteor.log("Received end of stream, will not reconnect"); |
226 |
andrew.betts |
32 |
Meteor.callbacks["eof"](); |
227 |
andrew.betts |
55 |
Meteor.setstatus(6); |
228 |
andrew.betts |
53 |
Meteor.disconnect(); |
229 |
andrew.betts |
32 |
}, |
230 |
|
|
|
231 |
andrew.betts |
53 |
channelInfo: function(channel, id) { |
232 |
|
|
Meteor.channels[channel].lastmsgreceived = id; |
233 |
|
|
Meteor.log("Received channel info for channel "+channel+": resume from "+id); |
234 |
|
|
}, |
235 |
|
|
|
236 |
andrew.betts |
32 |
updatepollfreq: function() { |
237 |
|
|
var now = new Date(); |
238 |
|
|
var t = now.getTime(); |
239 |
|
|
var avg = 0; |
240 |
|
|
for (var i=1; i<Meteor.recvtimes.length; i++) { |
241 |
|
|
avg += (Meteor.recvtimes[i]-Meteor.recvtimes[i-1]); |
242 |
|
|
} |
243 |
|
|
avg += (t-Meteor.recvtimes[Meteor.recvtimes.length-1]); |
244 |
|
|
avg /= Meteor.recvtimes.length; |
245 |
|
|
var target = avg/2; |
246 |
|
|
if (target < Meteor.pollfreq && Meteor.pollfreq > Meteor.minpollfreq) Meteor.pollfreq = Math.ceil(Meteor.pollfreq*0.9); |
247 |
|
|
if (target > Meteor.pollfreq && Meteor.pollfreq < Meteor.maxpollfreq) Meteor.pollfreq = Math.floor(Meteor.pollfreq*1.05); |
248 |
|
|
}, |
249 |
|
|
|
250 |
|
|
registerEventCallback: function(evt, funcRef) { |
251 |
|
|
Function.prototype.andThen=function(g) { |
252 |
|
|
var f=this; |
253 |
|
|
var a=Meteor.arguments |
254 |
|
|
return function(args) { |
255 |
|
|
f(a);g(args); |
256 |
|
|
} |
257 |
|
|
}; |
258 |
|
|
if (typeof Meteor.callbacks[evt] == "function") { |
259 |
|
|
Meteor.callbacks[evt] = (Meteor.callbacks[evt]).andThen(funcRef); |
260 |
|
|
} else { |
261 |
|
|
Meteor.callbacks[evt] = funcRef; |
262 |
|
|
} |
263 |
|
|
}, |
264 |
|
|
|
265 |
|
|
frameloadtimeout: function() { |
266 |
|
|
Meteor.log("Frame load timeout"); |
267 |
|
|
if (Meteor.frameloadtimer) clearTimeout(Meteor.frameloadtimer); |
268 |
|
|
Meteor.setstatus(3); |
269 |
andrew.betts |
53 |
Meteor.pollmode(); |
270 |
andrew.betts |
32 |
}, |
271 |
|
|
|
272 |
|
|
extract_xss_domain: function(old_domain) { |
273 |
|
|
if (old_domain.match(/^(\d{1,3}\.){3}\d{1,3}$/)) return old_domain; |
274 |
|
|
domain_pieces = old_domain.split('.'); |
275 |
|
|
return domain_pieces.slice(-2, domain_pieces.length).join("."); |
276 |
|
|
}, |
277 |
|
|
|
278 |
|
|
setstatus: function(newstatus) { |
279 |
|
|
// Statuses: 0 = Uninitialised, |
280 |
|
|
// 1 = Loading stream, |
281 |
|
|
// 2 = Loading controller frame, |
282 |
|
|
// 3 = Controller frame timeout, retrying. |
283 |
|
|
// 4 = Controller frame loaded and ready |
284 |
|
|
// 5 = Receiving data |
285 |
andrew.betts |
55 |
// 6 = End of stream, will not reconnect |
286 |
andrew.betts |
32 |
|
287 |
|
|
if (Meteor.status != newstatus) { |
288 |
|
|
Meteor.status = newstatus; |
289 |
|
|
Meteor.callbacks["statuschanged"](newstatus); |
290 |
|
|
} |
291 |
|
|
}, |
292 |
|
|
|
293 |
|
|
log: function(logstr) { |
294 |
|
|
if (Meteor.debugmode) { |
295 |
|
|
if (window.console) { |
296 |
|
|
window.console.log(logstr); |
297 |
|
|
} else if (document.getElementById("meteorlogoutput")) { |
298 |
|
|
document.getElementById("meteorlogoutput").innerHTML += logstr+"<br/>"; |
299 |
|
|
} |
300 |
|
|
} |
301 |
|
|
} |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
var oldonunload = window.onunload; |
305 |
|
|
if (typeof window.onunload != 'function') { |
306 |
|
|
window.onunload = Meteor.disconnect; |
307 |
|
|
} else { |
308 |
|
|
window.onunload = function() { |
309 |
|
|
if (oldonunload) oldonunload(); |
310 |
|
|
Meteor.disconnect(); |
311 |
|
|
} |
312 |
|
|
} |