Including the node_modules folder for socket.io code.
[KiwiIRC.git] / node / node_modules / socket.io-client / lib / transports / jsonp-polling.js
1
2 /**
3 * socket.io
4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5 * MIT Licensed
6 */
7
8 (function (exports, io) {
9
10 /**
11 * Expose constructor.
12 */
13
14 exports['jsonp-polling'] = JSONPPolling;
15
16 /**
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.
21 *
22 * @constructor
23 * @extends {io.Transport.xhr-polling}
24 * @api public
25 */
26
27 function JSONPPolling (socket) {
28 io.Transport['xhr-polling'].apply(this, arguments);
29
30 this.index = io.j.length;
31
32 var self = this;
33
34 io.j.push(function (msg) {
35 self._(msg);
36 });
37 };
38
39 /**
40 * Inherits from XHR polling transport.
41 */
42
43 io.util.inherit(JSONPPolling, io.Transport['xhr-polling']);
44
45 /**
46 * Transport name
47 *
48 * @api public
49 */
50
51 JSONPPolling.prototype.name = 'jsonp-polling';
52
53 /**
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.
58 *
59 * @param {String} data A encoded message.
60 * @api private
61 */
62
63 JSONPPolling.prototype.post = function (data) {
64 var self = this;
65
66 if (!this.form) {
67 var form = document.createElement('FORM')
68 , area = document.createElement('TEXTAREA')
69 , id = this.iframeId = 'socketio_iframe_' + this.index
70 , iframe;
71
72 form.className = 'socketio';
73 form.style.position = 'absolute';
74 form.style.top = '-1000px';
75 form.style.left = '-1000px';
76 form.target = id;
77 form.method = 'POST';
78 area.name = 'd';
79 form.appendChild(area);
80 document.body.appendChild(form);
81
82 this.form = form;
83 this.area = area;
84 }
85
86 this.form.action = this.prepareUrl() + '?t=' + (+new Date) + '&i=' + this.index;
87
88 function complete () {
89 initIframe();
90 self.socket.setBuffer(false);
91 };
92
93 function initIframe () {
94 if (self.iframe) {
95 self.form.removeChild(self.iframe);
96 }
97
98 try {
99 // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
100 iframe = document.createElement('<iframe name="'+ self.iframeId +'">');
101 } catch (e) {
102 iframe = document.createElement('iframe');
103 iframe.name = self.iframeId;
104 }
105
106 iframe.id = self.iframeId;
107
108 self.form.appendChild(iframe);
109 self.iframe = iframe;
110 };
111
112 initIframe();
113
114 this.area.value = data;
115
116 try {
117 this.form.submit();
118 } catch(e) {}
119
120 if (this.iframe.attachEvent) {
121 iframe.onreadystatechange = function () {
122 if (self.iframe.readyState == 'complete') {
123 complete();
124 }
125 };
126 } else {
127 this.iframe.onload = complete;
128 }
129 };
130
131 /**
132 * Creates a new JSONP poll that can be used to listen
133 * for messages from the Socket.IO server.
134 *
135 * @api private
136 */
137
138 JSONPPolling.prototype.get = function () {
139 var self = this
140 , script = document.createElement('SCRIPT');
141
142 if (this.script) {
143 this.script.parentNode.removeChild(this.script);
144 this.script = null;
145 }
146
147 script.async = true;
148 script.src = this.prepareUrl() + '/?t=' + (+new Date) + '&i=' + this.index;
149 script.onerror = function () {
150 self.onClose();
151 };
152
153 var insertAt = document.getElementsByTagName('script')[0]
154 insertAt.parentNode.insertBefore(script, insertAt);
155 this.script = script;
156 };
157
158 /**
159 * Callback function for the incoming message stream from the Socket.IO server.
160 *
161 * @param {String} data The message
162 * @api private
163 */
164
165 JSONPPolling.prototype._ = function (msg) {
166 this.onData(msg);
167 if (this.open) {
168 this.get();
169 }
170 return this;
171 };
172
173 /**
174 * Checks if browser supports this transport.
175 *
176 * @return {Boolean}
177 * @api public
178 */
179
180 JSONPPolling.check = function () {
181 return true;
182 };
183
184 /**
185 * Check if cross domain requests are supported
186 *
187 * @returns {Boolean}
188 * @api public
189 */
190
191 JSONPPolling.xdomainCheck = function () {
192 return true;
193 };
194
195 /**
196 * Add the transport to your public io.transports array.
197 *
198 * @api private
199 */
200
201 io.transports.push('jsonp-polling');
202
203 })(
204 'undefined' != typeof io ? io.Transport : module.exports
205 , 'undefined' != typeof io ? io : module.parent.exports
206 );