|
/**
* socket.io
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
* MIT Licensed
*/
(function (exports, io, global) {
/**
* Expose constructor.
*
* @api public
*/
exports.XHR = XHR;
/**
* XHR constructor
*
* @costructor
* @api public
*/
function XHR (socket) {
if (!socket) return;
io.Transport.apply(this, arguments);
this.sendBuffer = [];
};
/**
* Inherits from Transport.
*/
io.util.inherit(XHR, io.Transport);
/**
* Establish a connection
*
* @returns {Transport}
* @api public
*/
XHR.prototype.open = function () {
this.socket.setBuffer(false);
this.onOpen();
this.get();
// we need to make sure the request succeeds since we have no indication
// whether the request opened or not until it succeeded.
this.setCloseTimeout();
return this;
};
/**
* Check if we need to send data to the Socket.IO server, if we have data in our
* buffer we encode it and forward it to the `post` method.
*
* @api private
*/
XHR.prototype.payload = function (payload) {
var msgs = [];
for (var i = 0, l = payload.length; i < l; i++) {
msgs.push(io.parser.encodePacket(payload[i]));
}
this.send(io.parser.encodePayload(msgs));
};
/**
* Send data to the Socket.IO server.
*
* @param data The message
* @returns {Transport}
* @api public
*/
XHR.prototype.send = function (data) {
this.post(data);
return this;
};
/**
* Posts a encoded message to the Socket.IO server.
*
* @param {String} data A encoded message.
* @api private
*/
function empty () { };
XHR.prototype.post = function (data) {
var self = this;
this.socket.setBuffer(true);
function stateChange () {
if (this.readyState == 4) {
this.onreadystatechange = empty;
self.posting = false;
if (this.status == 200){
self.socket.setBuffer(false);
} else {
self.onClose();
}
}
}
function onload () {
this.onload = empty;
self.socket.setBuffer(false);
};
this.sendXHR = this.request('POST');
if (global.XDomainRequest && this.sendXHR instanceof XDomainRequest) {
this.sendXHR.onload = this.sendXHR.onerror = onload;
} else {
this.sendXHR.onreadystatechange = stateChange;
}
this.sendXHR.send(data);
};
/**
* Disconnects the established `XHR` connection.
*
* @returns {Transport}
* @api public
*/
XHR.prototype.close = function () {
this.onClose();
return this;
};
/**
* Generates a configured XHR request
*
* @param {String} url The url that needs to be requested.
* @param {String} method The method the request should use.
* @returns {XMLHttpRequest}
* @api private
*/
XHR.prototype.request = function (method) {
var req = io.util.request(this.socket.isXDomain())
, query = io.util.query(this.socket.options.query, 't=' + +new Date);
req.open(method || 'GET', this.prepareUrl() + query, true);
if (method == 'POST') {
try {
if (req.setRequestHeader) {
req.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
} else {
// XDomainRequest
req.contentType = 'text/plain';
}
} catch (e) {}
}
return req;
};
/**
* Returns the scheme to use for the transport URLs.
*
* @api private
*/
XHR.prototype.scheme = function () {
return this.socket.options.secure ? 'https' : 'http';
};
/**
* Check if the XHR transports are supported
*
* @param {Boolean} xdomain Check if we support cross domain requests.
* @returns {Boolean}
* @api public
*/
XHR.check = function (socket, xdomain) {
try {
var request = io.util.request(xdomain),
usesXDomReq = (global.XDomainRequest && request instanceof XDomainRequest),
socketProtocol = (socket && socket.options && socket.options.secure ? 'https:' : 'http:'),
isXProtocol = (global.location && socketProtocol != global.location.protocol);
if (request && !(usesXDomReq && isXProtocol)) {
return true;
}
} catch(e) {}
return false;
};
/**
* Check if the XHR transport supports cross domain requests.
*
* @returns {Boolean}
* @api public
*/
XHR.xdomainCheck = function (socket) {
return XHR.check(socket, true);
};
})(
'undefined' != typeof io ? io.Transport : module.exports
, 'undefined' != typeof io ? io : module.parent.exports
, this
);
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog