Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / transports / websocket.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.websocket = WS;
15
16   /**
17    * The WebSocket transport uses the HTML5 WebSocket API to establish an
18    * persistent connection with the Socket.IO server. This transport will also
19    * be inherited by the FlashSocket fallback as it provides a API compatible
20    * polyfill for the WebSockets.
21    *
22    * @constructor
23    * @extends {io.Transport}
24    * @api public
25    */
26
27   function WS (socket) {
28     io.Transport.apply(this, arguments);
29   };
30
31   /**
32    * Inherits from Transport.
33    */
34
35   io.util.inherit(WS, io.Transport);
36
37   /**
38    * Transport name
39    *
40    * @api public
41    */
42
43   WS.prototype.name = 'websocket';
44
45   /**
46    * Initializes a new `WebSocket` connection with the Socket.IO server. We attach
47    * all the appropriate listeners to handle the responses from the server.
48    *
49    * @returns {Transport}
50    * @api public
51    */
52
53   WS.prototype.open = function () {
54     var query = io.util.query(this.socket.options.query)
55       , self = this
56       , Socket
57
58     // if node
59     Socket = require('ws');
60     // end node
61
62     if (!Socket) {
63       Socket = global.MozWebSocket || global.WebSocket;
64     }
65
66     this.websocket = new Socket(this.prepareUrl() + query);
67
68     this.websocket.onopen = function () {
69       self.onOpen();
70       self.socket.setBuffer(false);
71     };
72     this.websocket.onmessage = function (ev) {
73       self.onData(ev.data);
74     };
75     this.websocket.onclose = function () {
76       self.onClose();
77       self.socket.setBuffer(true);
78     };
79     this.websocket.onerror = function (e) {
80       self.onError(e);
81     };
82
83     return this;
84   };
85
86   /**
87    * Send a message to the Socket.IO server. The message will automatically be
88    * encoded in the correct message format.
89    *
90    * @returns {Transport}
91    * @api public
92    */
93
94   // Do to a bug in the current IDevices browser, we need to wrap the send in a 
95   // setTimeout, when they resume from sleeping the browser will crash if 
96   // we don't allow the browser time to detect the socket has been closed
97   if (io.util.ua.iDevice) {
98     WS.prototype.send = function (data) {
99       var self = this;
100       setTimeout(function() {
101          self.websocket.send(data);
102       },0);
103       return this;
104     };
105   } else {
106     WS.prototype.send = function (data) {
107       this.websocket.send(data);
108       return this;
109     };
110   }
111
112   /**
113    * Payload
114    *
115    * @api private
116    */
117
118   WS.prototype.payload = function (arr) {
119     for (var i = 0, l = arr.length; i < l; i++) {
120       this.packet(arr[i]);
121     }
122     return this;
123   };
124
125   /**
126    * Disconnect the established `WebSocket` connection.
127    *
128    * @returns {Transport}
129    * @api public
130    */
131
132   WS.prototype.close = function () {
133     this.websocket.close();
134     return this;
135   };
136
137   /**
138    * Handle the errors that `WebSocket` might be giving when we
139    * are attempting to connect or send messages.
140    *
141    * @param {Error} e The error.
142    * @api private
143    */
144
145   WS.prototype.onError = function (e) {
146     this.socket.onError(e);
147   };
148
149   /**
150    * Returns the appropriate scheme for the URI generation.
151    *
152    * @api private
153    */
154   WS.prototype.scheme = function () {
155     return this.socket.options.secure ? 'wss' : 'ws';
156   };
157
158   /**
159    * Checks if the browser has support for native `WebSockets` and that
160    * it's not the polyfill created for the FlashSocket transport.
161    *
162    * @return {Boolean}
163    * @api public
164    */
165
166   WS.check = function () {
167     // if node
168     return true;
169     // end node
170     return ('WebSocket' in global && !('__addTask' in WebSocket))
171           || 'MozWebSocket' in global;
172   };
173
174   /**
175    * Check if the `WebSocket` transport support cross domain communications.
176    *
177    * @returns {Boolean}
178    * @api public
179    */
180
181   WS.xdomainCheck = function () {
182     return true;
183   };
184
185   /**
186    * Add the transport to your public io.transports array.
187    *
188    * @api private
189    */
190
191   io.transports.push('websocket');
192
193 })(
194     'undefined' != typeof io ? io.Transport : module.exports
195   , 'undefined' != typeof io ? io : module.parent.exports
196   , this
197 );