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