]>
Dogcows Code - chaz/chatty/blob - extra/cometd/public_html/meteor.js
2 stream: xhrinteractive, iframe, serversent
11 process: function() {},
14 statuschanged: function() {},
15 changemode: function() {}
33 updatepollfreqtimer: null,
35 register: function(ifr
) {
36 ifr
.p
= Meteor
.process
;
39 ifr
.ch
= Meteor
.channelInfo
;
40 clearTimeout(Meteor
.frameloadtimer
);
42 Meteor
.log("Frame registered");
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();
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
--;
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();
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());
78 Meteor
.loadFrame("http://"+Meteor
.host
+((Meteor
.port
==80)?"":":"+Meteor
.port
)+"/stream.html");
80 clearTimeout(Meteor
.pingtimer
);
81 Meteor
.pingtimer
= setTimeout(Meteor
.pollmode
, Meteor
.pingtimeout
);
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
;
90 Meteor
.lastrequest
= t
;
93 disconnect: function() {
95 clearTimeout(Meteor
.pingtimer
);
96 clearTimeout(Meteor
.updatepollfreqtimer
);
97 clearTimeout(Meteor
.frameloadtimer
);
98 if (typeof CollectGarbage
== 'function') CollectGarbage();
99 if (Meteor
.status
!= 6) Meteor
.setstatus(0);
100 Meteor
.log("Disconnected");
104 selectStreamTransport: function() {
106 var test
= ActiveXObject
;
109 if ((typeof window
.addEventStream
) == "function") return "iframe";
110 return "xhrinteractive";
113 getSubsUrl: function() {
114 var surl
= "http://" + Meteor
.host
+ ((Meteor
.port
==80)?"":":"+Meteor
.port
) + "/push/" + Meteor
.hostid
+ "/" + Meteor
.mode
;
115 for (var c
in Meteor
.channels
) {
117 if (Meteor
.channels
[c
].lastmsgreceived
> 0) {
118 surl
+= ".r"+(Meteor
.channels
[c
].lastmsgreceived
+1);
119 } else if (Meteor
.channels
[c
].backtrack
> 0) {
120 surl
+= ".b"+Meteor
.channels
[c
].backtrack
;
121 } else if (Meteor
.channels
[c
].backtrack
< 0 || isNaN(Meteor
.channels
[c
].backtrack
)) {
125 var now
= new Date();
126 surl
+= "?nc="+now
.getTime();
130 loadFrame: function(url
) {
132 if (!Meteor
.frameref
) {
133 var transferDoc
= new ActiveXObject("htmlfile");
134 Meteor
.frameref
= transferDoc
;
136 Meteor
.frameref
.open();
137 Meteor
.frameref
.write("<html><script>");
138 Meteor
.frameref
.write("document.domain=\""+(document
.domain
)+"\";");
139 Meteor
.frameref
.write("</"+"script></html>");
140 Meteor
.frameref
.parentWindow
.Meteor
= Meteor
;
141 Meteor
.frameref
.close();
142 var ifrDiv
= Meteor
.frameref
.createElement("div");
143 Meteor
.frameref
.appendChild(ifrDiv
);
144 ifrDiv
.innerHTML
= "<iframe src=\""+url
+"\"></iframe>";
146 if (!Meteor
.frameref
) {
147 var ifr
= document
.createElement("IFRAME");
148 ifr
.style
.width
= "10px";
149 ifr
.style
.height
= "10px";
150 ifr
.style
.border
= "none";
151 ifr
.style
.position
= "absolute";
152 ifr
.style
.top
= "-10px";
153 ifr
.style
.marginTop
= "-10px";
154 ifr
.style
.zIndex
= "-20";
156 document
.body
.appendChild(ifr
);
157 Meteor
.frameref
= ifr
;
159 Meteor
.frameref
.setAttribute("src", url
);
161 Meteor
.log("Loading URL '"+url
+"' into frame...");
162 Meteor
.frameloadtimer
= setTimeout(Meteor
.frameloadtimeout
, 5000);
165 pollmode: function() {
166 Meteor
.log("Ping timeout");
167 Meteor
.mode
="smartpoll";
168 clearTimeout(Meteor
.pingtimer
);
169 Meteor
.callbacks
["changemode"]("poll");
170 Meteor
.lastpingtime
= false;
174 process: function(id
, channel
, data
) {
178 } else if (typeof(Meteor
.channels
[channel
]) != "undefined") {
179 Meteor
.log("Message "+id
+" received on channel "+channel
+" (last id on channel: "+Meteor
.channels
[channel
].lastmsgreceived
+")\n"+data
);
180 Meteor
.callbacks
["process"](data
);
181 Meteor
.channels
[channel
].lastmsgreceived
= id
;
182 if (Meteor
.mode
=="smartpoll") {
183 var now
= new Date();
184 Meteor
.recvtimes
[Meteor
.recvtimes
.length
] = now
.getTime();
185 while (Meteor
.recvtimes
.length
> 5) Meteor
.recvtimes
.shift();
192 if (Meteor
.pingtimer
) {
193 clearTimeout(Meteor
.pingtimer
);
194 Meteor
.pingtimer
= setTimeout(Meteor
.pollmode
, Meteor
.pingtimeout
);
195 var now
= new Date();
196 Meteor
.lastpingtime
= now
.getTime();
202 if (Meteor
.status
!= 6) {
203 Meteor
.log("Stream reset");
205 Meteor
.callbacks
["reset"]();
206 var now
= new Date();
207 var t
= now
.getTime();
208 var x
= Meteor
.pollfreq
- (t
-Meteor
.lastrequest
);
210 setTimeout(Meteor
.connect
, x
);
215 Meteor
.log("Received end of stream, will not reconnect");
216 Meteor
.callbacks
["eof"]();
221 channelInfo: function(channel
, id
) {
222 Meteor
.channels
[channel
].lastmsgreceived
= id
;
223 Meteor
.log("Received channel info for channel "+channel
+": resume from "+id
);
226 updatepollfreq: function() {
227 var now
= new Date();
228 var t
= now
.getTime();
230 for (var i
=1; i
<Meteor
.recvtimes
.length
; i
++) {
231 avg
+= (Meteor
.recvtimes
[i
]-Meteor
.recvtimes
[i
-1]);
233 avg
+= (t
-Meteor
.recvtimes
[Meteor
.recvtimes
.length
-1]);
234 avg
/= Meteor
.recvtimes
.length
;
236 if (target
< Meteor
.pollfreq
&& Meteor
.pollfreq
> Meteor
.minpollfreq
) Meteor
.pollfreq
= Math
.ceil(Meteor
.pollfreq
*0.9);
237 if (target
> Meteor
.pollfreq
&& Meteor
.pollfreq
< Meteor
.maxpollfreq
) Meteor
.pollfreq
= Math
.floor(Meteor
.pollfreq
*1.05);
240 registerEventCallback: function(evt
, funcRef
) {
241 Function
.prototype.andThen=function(g
) {
243 var a
=Meteor
.arguments
244 return function(args
) {
248 if (typeof Meteor
.callbacks
[evt
] == "function") {
249 Meteor
.callbacks
[evt
] = (Meteor
.callbacks
[evt
]).andThen(funcRef
);
251 Meteor
.callbacks
[evt
] = funcRef
;
255 frameloadtimeout: function() {
256 Meteor
.log("Frame load timeout");
257 if (Meteor
.frameloadtimer
) clearTimeout(Meteor
.frameloadtimer
);
262 extract_xss_domain: function(old_domain
) {
263 if (old_domain
.match(/^(\d{1,3}\.){3}\d{1,3}$/)) return old_domain
;
264 domain_pieces
= old_domain
.split('.');
265 return domain_pieces
.slice(-2, domain_pieces
.length
).join(".");
268 setstatus: function(newstatus
) {
269 // Statuses: 0 = Uninitialised,
270 // 1 = Loading stream,
271 // 2 = Loading controller frame,
272 // 3 = Controller frame timeout, retrying.
273 // 4 = Controller frame loaded and ready
274 // 5 = Receiving data
275 // 6 = End of stream, will not reconnect
277 if (Meteor
.status
!= newstatus
) {
278 Meteor
.status
= newstatus
;
279 Meteor
.callbacks
["statuschanged"](newstatus
);
283 log: function(logstr
) {
284 if (Meteor
.debugmode
) {
285 if (window
.console
) {
286 window
.console
.log(logstr
);
287 } else if (document
.getElementById("meteorlogoutput")) {
288 document
.getElementById("meteorlogoutput").innerHTML
+= logstr
+"<br/>";
294 var oldonunload
= window
.onunload
;
295 if (typeof window
.onunload
!= 'function') {
296 window
.onunload
= Meteor
.disconnect
;
298 window
.onunload = function() {
299 if (oldonunload
) oldonunload();
This page took 0.052844 seconds and 4 git commands to generate.