4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
8 (function (exports
, io
) {
14 exports
['jsonp-polling'] = JSONPPolling
;
17 * The JSONP transport creates an persistent connection by dynamically
18 * inserting a script tag in the page. This script tag will receive the
19 * information of the Socket.IO server. When new information is received
20 * it creates a new script tag for the new data stream.
23 * @extends {io.Transport.xhr-polling}
27 function JSONPPolling (socket
) {
28 io
.Transport
['xhr-polling'].apply(this, arguments
);
30 this.index
= io
.j
.length
;
34 io
.j
.push(function (msg
) {
40 * Inherits from XHR polling transport.
43 io
.util
.inherit(JSONPPolling
, io
.Transport
['xhr-polling']);
51 JSONPPolling
.prototype.name
= 'jsonp-polling';
54 * Posts a encoded message to the Socket.IO server using an iframe.
55 * The iframe is used because script tags can create POST based requests.
56 * The iframe is positioned outside of the view so the user does not
57 * notice it's existence.
59 * @param {String} data A encoded message.
63 JSONPPolling
.prototype.post = function (data
) {
67 var form
= document
.createElement('FORM')
68 , area
= document
.createElement('TEXTAREA')
69 , id
= this.iframeId
= 'socketio_iframe_' + this.index
72 form
.className
= 'socketio';
73 form
.style
.position
= 'absolute';
74 form
.style
.top
= '-1000px';
75 form
.style
.left
= '-1000px';
79 form
.appendChild(area
);
80 document
.body
.appendChild(form
);
86 this.form
.action
= this.prepareUrl() + '?t=' + (+new Date
) + '&i=' + this.index
;
88 function complete () {
90 self
.socket
.setBuffer(false);
93 function initIframe () {
95 self
.form
.removeChild(self
.iframe
);
99 // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
100 iframe
= document
.createElement('<iframe name="'+ self
.iframeId
+'">');
102 iframe
= document
.createElement('iframe');
103 iframe
.name
= self
.iframeId
;
106 iframe
.id
= self
.iframeId
;
108 self
.form
.appendChild(iframe
);
109 self
.iframe
= iframe
;
114 this.area
.value
= data
;
120 if (this.iframe
.attachEvent
) {
121 iframe
.onreadystatechange = function () {
122 if (self
.iframe
.readyState
== 'complete') {
127 this.iframe
.onload
= complete
;
132 * Creates a new JSONP poll that can be used to listen
133 * for messages from the Socket.IO server.
138 JSONPPolling
.prototype.get = function () {
140 , script
= document
.createElement('SCRIPT');
143 this.script
.parentNode
.removeChild(this.script
);
148 script
.src
= this.prepareUrl() + '/?t=' + (+new Date
) + '&i=' + this.index
;
149 script
.onerror = function () {
153 var insertAt
= document
.getElementsByTagName('script')[0]
154 insertAt
.parentNode
.insertBefore(script
, insertAt
);
155 this.script
= script
;
159 * Callback function for the incoming message stream from the Socket.IO server.
161 * @param {String} data The message
165 JSONPPolling
.prototype._ = function (msg
) {
174 * Checks if browser supports this transport.
180 JSONPPolling
.check = function () {
185 * Check if cross domain requests are supported
191 JSONPPolling
.xdomainCheck = function () {
196 * Add the transport to your public io.transports array.
201 io
.transports
.push('jsonp-polling');
204 'undefined' != typeof io
? io
.Transport
: module
.exports
205 , 'undefined' != typeof io
? io
: module
.parent
.exports