Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / components / learnboost-engine.io-client / lib / transports / websocket.js
1
2 /**
3  * Module dependencies.
4  */
5
6 var Transport = require('../transport')
7   , parser = require('../parser')
8   , util = require('../util')
9   , debug = require('debug')('engine.io-client:websocket');
10
11 /**
12  * Module exports.
13  */
14
15 module.exports = WS;
16
17 /**
18  * Global reference.
19  */
20
21 var global = 'undefined' != typeof window ? window : global;
22
23 /**
24  * WebSocket transport constructor.
25  *
26  * @api {Object} connection options
27  * @api public
28  */
29
30 function WS(opts){
31   Transport.call(this, opts);
32 };
33
34 /**
35  * Inherits from Transport.
36  */
37
38 util.inherits(WS, Transport);
39
40 /**
41  * Transport name.
42  *
43  * @api public
44  */
45
46 WS.prototype.name = 'websocket';
47
48 /**
49  * Opens socket.
50  *
51  * @api private
52  */
53
54 WS.prototype.doOpen = function(){
55   if (!this.check()) {
56     // let probe timeout
57     return;
58   }
59
60   var self = this;
61
62   this.socket = new (ws())(this.uri());
63   this.socket.onopen = function(){
64     self.onOpen();
65   };
66   this.socket.onclose = function(){
67     self.onClose();
68   };
69   this.socket.onmessage = function(ev){
70     self.onData(ev.data);
71   };
72   this.socket.onerror = function(e){
73     self.onError('websocket error', e);
74   };
75 };
76
77 /**
78  * Writes data to socket.
79  *
80  * @param {Array} array of packets.
81  * @api private
82  */
83
84 WS.prototype.write = function(packets){
85   for (var i = 0, l = packets.length; i < l; i++) {
86     this.socket.send(parser.encodePacket(packets[i]));
87   }
88 };
89
90 /**
91  * Closes socket.
92  *
93  * @api private
94  */
95
96 WS.prototype.doClose = function(){
97   if (typeof this.socket !== 'undefined') {
98     this.socket.close();
99   }
100 };
101
102 /**
103  * Generates uri for connection.
104  *
105  * @api private
106  */
107
108 WS.prototype.uri = function(){
109   var query = this.query || {};
110   var schema = this.secure ? 'wss' : 'ws';
111   var port = '';
112
113   // avoid port if default for schema
114   if (this.port && (('wss' == schema && this.port != 443)
115     || ('ws' == schema && this.port != 80))) {
116     port = ':' + this.port;
117   }
118
119   // append timestamp to URI
120   if (this.timestampRequests) {
121     query[this.timestampParam] = +new Date;
122   }
123
124   query = util.qs(query);
125
126   // prepend ? to query
127   if (query.length) {
128     query = '?' + query;
129   }
130
131   return schema + '://' + this.host + port + this.path + query;
132 };
133
134 /**
135  * Feature detection for WebSocket.
136  *
137  * @return {Boolean} whether this transport is available.
138  * @api public
139  */
140
141 WS.prototype.check = function(){
142   var websocket = ws();
143   return !!websocket && !('__initialize' in websocket && this.name === WS.prototype.name);
144 };
145
146 /**
147  * Getter for WS constructor.
148  *
149  * @api private
150  */
151
152 function ws(){
153   if ('undefined' != typeof process) {
154     return require('ws');
155   }
156
157   return global.WebSocket || global.MozWebSocket;
158 }