PageRenderTime 244ms CodeModel.GetById 120ms app.highlight 9ms RepoModel.GetById 112ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 120 lines | 91 code | 21 blank | 8 comment | 7 complexity | 15d1bf9412804f2f2b6777ec0f2bc95f 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	io.JSONP = [];
 13	
 14	JSONPPolling = io.Transport['jsonp-polling'] = function(){
 15		io.Transport.XHR.apply(this, arguments);
 16		this._insertAt = document.getElementsByTagName('script')[0];
 17		this._index = io.JSONP.length;
 18		io.JSONP.push(this);
 19	};
 20	
 21	io.util.inherit(JSONPPolling, io.Transport['xhr-polling']);
 22	
 23	JSONPPolling.prototype.type = 'jsonp-polling';
 24	
 25	JSONPPolling.prototype._send = function(data){
 26		var self = this;
 27		if (!('_form' in this)){
 28			var form = document.createElement('FORM'),
 29				area = document.createElement('TEXTAREA'),
 30				id = this._iframeId = 'socket_io_iframe_' + this._index,
 31				iframe;
 32	
 33			form.style.position = 'absolute';
 34			form.style.top = '-1000px';
 35			form.style.left = '-1000px';
 36			form.target = id;
 37			form.method = 'POST';
 38			form.action = this._prepareUrl() + '/' + (+new Date) + '/' + this._index;
 39			area.name = 'data';
 40			form.appendChild(area);
 41			this._insertAt.parentNode.insertBefore(form, this._insertAt);
 42			document.body.appendChild(form);
 43	
 44			this._form = form;
 45			this._area = area;
 46		}
 47	
 48		function complete(){
 49			initIframe();
 50			self._posting = false;
 51			self._checkSend();
 52		};
 53	
 54		function initIframe(){
 55			if (self._iframe){
 56				self._form.removeChild(self._iframe);
 57			} 
 58	
 59			try {
 60				// ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
 61				iframe = document.createElement('<iframe name="'+ self._iframeId +'">');
 62			} catch(e){
 63				iframe = document.createElement('iframe');
 64				iframe.name = self._iframeId;
 65			}
 66	
 67			iframe.id = self._iframeId;
 68	
 69			self._form.appendChild(iframe);
 70			self._iframe = iframe;
 71		};
 72	
 73		initIframe();
 74	
 75		this._posting = true;
 76		this._area.value = data;
 77	
 78		try {
 79			this._form.submit();
 80		} catch(e){}
 81	
 82		if (this._iframe.attachEvent){
 83			iframe.onreadystatechange = function(){
 84				if (self._iframe.readyState == 'complete') complete();
 85			};
 86		} else {
 87			this._iframe.onload = complete;
 88		}
 89	};
 90	
 91	JSONPPolling.prototype._get = function(){
 92		var self = this,
 93				script = document.createElement('SCRIPT');
 94		if (this._script){
 95			this._script.parentNode.removeChild(this._script);
 96			this._script = null;
 97		}
 98		script.async = true;
 99		script.src = this._prepareUrl() + '/' + (+new Date) + '/' + this._index;
100		script.onerror = function(){
101			self._onDisconnect();
102		};
103		this._insertAt.parentNode.insertBefore(script, this._insertAt);
104		this._script = script;
105	};
106	
107	JSONPPolling.prototype._ = function(){
108		this._onData.apply(this, arguments);
109		this._get();
110		return this;
111	};
112	
113	JSONPPolling.check = function(){
114		return true;
115	};
116	
117	JSONPPolling.xdomainCheck = function(){
118		return true;
119	};
120})();