Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / transports / xhr-polling.js
1
2 /**
3  * socket.io
4  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5  * MIT Licensed
6  */
7
8 (function (exports, io, global) {
9
10   /**
11    * Expose constructor.
12    */
13
14   exports['xhr-polling'] = XHRPolling;
15
16   /**
17    * The XHR-polling transport uses long polling XHR requests to create a
18    * "persistent" connection with the server.
19    *
20    * @constructor
21    * @api public
22    */
23
24   function XHRPolling () {
25     io.Transport.XHR.apply(this, arguments);
26   };
27
28   /**
29    * Inherits from XHR transport.
30    */
31
32   io.util.inherit(XHRPolling, io.Transport.XHR);
33
34   /**
35    * Merge the properties from XHR transport
36    */
37
38   io.util.merge(XHRPolling, io.Transport.XHR);
39
40   /**
41    * Transport name
42    *
43    * @api public
44    */
45
46   XHRPolling.prototype.name = 'xhr-polling';
47
48   /**
49    * Indicates whether heartbeats is enabled for this transport
50    *
51    * @api private
52    */
53
54   XHRPolling.prototype.heartbeats = function () {
55     return false;
56   };
57
58   /** 
59    * Establish a connection, for iPhone and Android this will be done once the page
60    * is loaded.
61    *
62    * @returns {Transport} Chaining.
63    * @api public
64    */
65
66   XHRPolling.prototype.open = function () {
67     var self = this;
68
69     io.Transport.XHR.prototype.open.call(self);
70     return false;
71   };
72
73   /**
74    * Starts a XHR request to wait for incoming messages.
75    *
76    * @api private
77    */
78
79   function empty () {};
80
81   XHRPolling.prototype.get = function () {
82     if (!this.isOpen) return;
83
84     var self = this;
85
86     function stateChange () {
87       if (this.readyState == 4) {
88         this.onreadystatechange = empty;
89
90         if (this.status == 200) {
91           self.onData(this.responseText);
92           self.get();
93         } else {
94           self.onClose();
95         }
96       }
97     };
98
99     function onload () {
100       this.onload = empty;
101       this.onerror = empty;
102       self.retryCounter = 1;
103       self.onData(this.responseText);
104       self.get();
105     };
106
107     function onerror () {
108       self.retryCounter ++;
109       if(!self.retryCounter || self.retryCounter > 3) {
110         self.onClose();  
111       } else {
112         self.get();
113       }
114     };
115
116     this.xhr = this.request();
117
118     if (global.XDomainRequest && this.xhr instanceof XDomainRequest) {
119       this.xhr.onload = onload;
120       this.xhr.onerror = onerror;
121     } else {
122       this.xhr.onreadystatechange = stateChange;
123     }
124
125     this.xhr.send(null);
126   };
127
128   /**
129    * Handle the unclean close behavior.
130    *
131    * @api private
132    */
133
134   XHRPolling.prototype.onClose = function () {
135     io.Transport.XHR.prototype.onClose.call(this);
136
137     if (this.xhr) {
138       this.xhr.onreadystatechange = this.xhr.onload = this.xhr.onerror = empty;
139       try {
140         this.xhr.abort();
141       } catch(e){}
142       this.xhr = null;
143     }
144   };
145
146   /**
147    * Webkit based browsers show a infinit spinner when you start a XHR request
148    * before the browsers onload event is called so we need to defer opening of
149    * the transport until the onload event is called. Wrapping the cb in our
150    * defer method solve this.
151    *
152    * @param {Socket} socket The socket instance that needs a transport
153    * @param {Function} fn The callback
154    * @api private
155    */
156
157   XHRPolling.prototype.ready = function (socket, fn) {
158     var self = this;
159
160     io.util.defer(function () {
161       fn.call(self);
162     });
163   };
164
165   /**
166    * Add the transport to your public io.transports array.
167    *
168    * @api private
169    */
170
171   io.transports.push('xhr-polling');
172
173 })(
174     'undefined' != typeof io ? io.Transport : module.exports
175   , 'undefined' != typeof io ? io : module.parent.exports
176   , this
177 );