PageRenderTime 124ms CodeModel.GetById 108ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/transports/xhr.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 136 lines | 109 code | 18 blank | 9 comment | 25 complexity | 5ba9a90f5995d37664a7e433eafb0130 MD5 | raw file
  1/**
  2 * Socket.IO client
  3 * 
  4 * @author Guillermo Rauch <guillermo@learnboost.com>
  5 * @license The MIT license.
  6 * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  7 */
  8
  9(function(){
 10	var io = this.io;
 11	
 12	var empty = new Function,
 13	    
 14	XMLHttpRequestCORS = (function(){
 15		if (!('XMLHttpRequest' in window)) return false;
 16		// CORS feature detection
 17		var a = new XMLHttpRequest();
 18		return a.withCredentials != undefined;
 19	})(),
 20	
 21	request = function(xdomain){
 22		if ('XDomainRequest' in window && xdomain) return new XDomainRequest();
 23		if ('XMLHttpRequest' in window && (!xdomain || XMLHttpRequestCORS)) return new XMLHttpRequest();
 24		if (!xdomain){
 25			try {
 26				var a = new ActiveXObject('MSXML2.XMLHTTP');
 27				return a;
 28			} catch(e){}
 29		
 30			try {
 31				var b = new ActiveXObject('Microsoft.XMLHTTP');
 32				return b;
 33			} catch(e){}
 34		}
 35		return false;
 36	},
 37	
 38	XHR = io.Transport.XHR = function(){
 39		io.Transport.apply(this, arguments);
 40		this._sendBuffer = [];
 41	};
 42	
 43	io.util.inherit(XHR, io.Transport);
 44	
 45	XHR.prototype.connect = function(){
 46		this._get();
 47		return this;
 48	};
 49	
 50	XHR.prototype._checkSend = function(){
 51		if (!this._posting && this._sendBuffer.length){
 52			var encoded = this._encode(this._sendBuffer);
 53			this._sendBuffer = [];
 54			this._send(encoded);
 55		}
 56	};
 57	
 58	XHR.prototype.send = function(data){
 59		if (io.util.isArray(data)){
 60			this._sendBuffer.push.apply(this._sendBuffer, data);
 61		} else {
 62			this._sendBuffer.push(data);
 63		}
 64		this._checkSend();
 65		return this;
 66	};
 67	
 68	XHR.prototype._send = function(data){
 69		var self = this;
 70		this._posting = true;
 71		this._sendXhr = this._request('send', 'POST');
 72		this._sendXhr.onreadystatechange = function(){
 73			var status;
 74			if (self._sendXhr.readyState == 4){
 75				self._sendXhr.onreadystatechange = empty;
 76				try { status = self._sendXhr.status; } catch(e){}
 77				self._posting = false;
 78				if (status == 200){
 79					self._checkSend();
 80				} else {
 81					self._onDisconnect();
 82				}
 83			}
 84		};
 85		this._sendXhr.send('data=' + encodeURIComponent(data));
 86	};
 87	
 88	XHR.prototype.disconnect = function(){
 89		// send disconnection signal
 90		this._onDisconnect();
 91		return this;
 92	};
 93	
 94	XHR.prototype._onDisconnect = function(){
 95		if (this._xhr){
 96			this._xhr.onreadystatechange = empty;
 97      try {
 98        this._xhr.abort();
 99      } catch(e){}
100			this._xhr = null;
101		}
102		if (this._sendXhr){
103      this._sendXhr.onreadystatechange = empty;
104      try {
105        this._sendXhr.abort();
106      } catch(e){}
107			this._sendXhr = null;
108		}
109		this._sendBuffer = [];
110		io.Transport.prototype._onDisconnect.call(this);
111	};
112	
113	XHR.prototype._request = function(url, method, multipart){
114		var req = request(this.base._isXDomain());
115		if (multipart) req.multipart = true;
116		req.open(method || 'GET', this._prepareUrl() + (url ? '/' + url : ''));
117		if (method == 'POST' && 'setRequestHeader' in req){
118			req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
119		}
120		return req;
121	};
122	
123	XHR.check = function(xdomain){
124		try {
125			if (request(xdomain)) return true;
126		} catch(e){}
127		return false;
128	};
129	
130	XHR.xdomainCheck = function(){
131		return XHR.check(true);
132	};
133	
134	XHR.request = request;
135	
136})();