PageRenderTime 53ms CodeModel.GetById 48ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 62 lines | 45 code | 10 blank | 7 comment | 8 complexity | ad825fe5b88319a77a1a50cda00c9dbf 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	XHRPolling = io.Transport['xhr-polling'] = function(){
15		io.Transport.XHR.apply(this, arguments);
16	};
17
18	io.util.inherit(XHRPolling, io.Transport.XHR);
19
20	XHRPolling.prototype.type = 'xhr-polling';
21
22	XHRPolling.prototype.connect = function(){
23		if (io.util.ios || io.util.android){
24			var self = this;
25			io.util.load(function(){
26				setTimeout(function(){
27					io.Transport.XHR.prototype.connect.call(self);
28				}, 10);
29			});
30		} else {
31			io.Transport.XHR.prototype.connect.call(this);
32		}
33	};
34
35	XHRPolling.prototype._get = function(){
36		var self = this;
37		this._xhr = this._request(+ new Date, 'GET');
38    this._xhr.onreadystatechange = function(){
39      var status;
40      if (self._xhr.readyState == 4){
41        self._xhr.onreadystatechange = empty;
42        try { status = self._xhr.status; } catch(e){}
43        if (status == 200){
44          self._onData(self._xhr.responseText);
45          self._get();
46        } else {
47          self._onDisconnect();
48        }
49      }
50    };
51		this._xhr.send(null);
52	};
53
54	XHRPolling.check = function(){
55		return io.Transport.XHR.check();
56	};
57
58	XHRPolling.xdomainCheck = function(){
59		return io.Transport.XHR.xdomainCheck();
60	};
61
62})();