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