forked from faye/faye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonp.js
47 lines (40 loc) · 1.39 KB
/
jsonp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
request: function(message, timeout) {
var params = {message: Faye.toJSON(message)},
head = document.getElementsByTagName('head')[0],
script = document.createElement('script'),
callbackName = Faye.Transport.JSONP.getCallbackName(),
location = Faye.URI.parse(this._endpoint, params),
self = this;
var removeScript = function() {
if (!script.parentNode) return false;
script.parentNode.removeChild(script);
return true;
};
Faye.ENV[callbackName] = function(data) {
Faye.ENV[callbackName] = undefined;
try { delete Faye.ENV[callbackName] } catch (e) {}
if (!removeScript()) return;
self.receive(data);
};
Faye.ENV.setTimeout(function() {
if (!Faye.ENV[callbackName]) return;
removeScript();
self.request(message, 2 * timeout);
}, 1000 * timeout);
location.params.jsonp = callbackName;
script.type = 'text/javascript';
script.src = location.toURL();
head.appendChild(script);
}
}), {
_cbCount: 0,
getCallbackName: function() {
this._cbCount += 1;
return '__jsonp' + this._cbCount + '__';
},
isUsable: function(endpoint, callback, scope) {
callback.call(scope, true);
}
});
Faye.Transport.register('callback-polling', Faye.Transport.JSONP);