4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
8 (function (exports, io, global) {
25 function XHR (socket) {
28 io.Transport.apply(this, arguments);
33 * Inherits from Transport.
36 io.util.inherit(XHR, io.Transport);
39 * Establish a connection
41 * @returns {Transport}
45 XHR.prototype.open = function () {
46 this.socket.setBuffer(false);
50 // we need to make sure the request succeeds since we have no indication
51 // whether the request opened or not until it succeeded.
52 this.setCloseTimeout();
58 * Check if we need to send data to the Socket.IO server, if we have data in our
59 * buffer we encode it and forward it to the `post` method.
64 XHR.prototype.payload = function (payload) {
67 for (var i = 0, l = payload.length; i < l; i++) {
68 msgs.push(io.parser.encodePacket(payload[i]));
71 this.send(io.parser.encodePayload(msgs));
75 * Send data to the Socket.IO server.
77 * @param data The message
78 * @returns {Transport}
82 XHR.prototype.send = function (data) {
88 * Posts a encoded message to the Socket.IO server.
90 * @param {String} data A encoded message.
94 function empty () { };
96 XHR.prototype.post = function (data) {
98 this.socket.setBuffer(true);
100 function stateChange () {
101 if (this.readyState == 4) {
102 this.onreadystatechange = empty;
103 self.posting = false;
105 if (this.status == 200){
106 self.socket.setBuffer(false);
115 self.socket.setBuffer(false);
118 this.sendXHR = this.request('POST');
120 if (global.XDomainRequest && this.sendXHR instanceof XDomainRequest) {
121 this.sendXHR.onload = this.sendXHR.onerror = onload;
123 this.sendXHR.onreadystatechange = stateChange;
126 this.sendXHR.send(data);
130 * Disconnects the established `XHR` connection.
132 * @returns {Transport}
136 XHR.prototype.close = function () {
142 * Generates a configured XHR request
144 * @param {String} url The url that needs to be requested.
145 * @param {String} method The method the request should use.
146 * @returns {XMLHttpRequest}
150 XHR.prototype.request = function (method) {
151 var req = io.util.request(this.socket.isXDomain())
152 , query = io.util.query(this.socket.options.query, 't=' + +new Date);
154 req.open(method || 'GET', this.prepareUrl() + query, true);
156 if (method == 'POST') {
158 if (req.setRequestHeader) {
159 req.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
162 req.contentType = 'text/plain';
171 * Returns the scheme to use for the transport URLs.
176 XHR.prototype.scheme = function () {
177 return this.socket.options.secure ? 'https' : 'http';
181 * Check if the XHR transports are supported
183 * @param {Boolean} xdomain Check if we support cross domain requests.
188 XHR.check = function (socket, xdomain) {
190 var request = io.util.request(xdomain),
191 usesXDomReq = (global.XDomainRequest && request instanceof XDomainRequest),
192 socketProtocol = (socket && socket.options && socket.options.secure ? 'https:' : 'http:'),
193 isXProtocol = (global.location && socketProtocol != global.location.protocol);
194 if (request && !(usesXDomReq && isXProtocol)) {
203 * Check if the XHR transport supports cross domain requests.
209 XHR.xdomainCheck = function (socket) {
210 return XHR.check(socket, true);
214 'undefined' != typeof io ? io.Transport : module.exports
215 , 'undefined' != typeof io ? io : module.parent.exports