@@ -8,13 +8,17 @@ var parseqs = require('parseqs');
8
8
var inherit = require ( 'component-inherit' ) ;
9
9
var yeast = require ( 'yeast' ) ;
10
10
var debug = require ( 'debug' ) ( 'engine.io-client:websocket' ) ;
11
+
11
12
var BrowserWebSocket , NodeWebSocket ;
12
- if ( typeof self === 'undefined' ) {
13
+
14
+ if ( typeof WebSocket !== 'undefined' ) {
15
+ BrowserWebSocket = WebSocket ;
16
+ } else if ( typeof self !== 'undefined' ) {
17
+ BrowserWebSocket = self . WebSocket || self . MozWebSocket ;
18
+ } else {
13
19
try {
14
20
NodeWebSocket = require ( 'ws' ) ;
15
21
} catch ( e ) { }
16
- } else {
17
- BrowserWebSocket = self . WebSocket || self . MozWebSocket ;
18
22
}
19
23
20
24
/**
@@ -23,7 +27,7 @@ if (typeof self === 'undefined') {
23
27
* interface exposed by `ws` for Node-like environment.
24
28
*/
25
29
26
- var WebSocket = BrowserWebSocket || NodeWebSocket ;
30
+ var WebSocketImpl = BrowserWebSocket || NodeWebSocket ;
27
31
28
32
/**
29
33
* Module exports.
@@ -47,7 +51,7 @@ function WS (opts) {
47
51
this . usingBrowserWebSocket = BrowserWebSocket && ! opts . forceNode ;
48
52
this . protocols = opts . protocols ;
49
53
if ( ! this . usingBrowserWebSocket ) {
50
- WebSocket = NodeWebSocket ;
54
+ WebSocketImpl = NodeWebSocket ;
51
55
}
52
56
Transport . call ( this , opts ) ;
53
57
}
@@ -107,7 +111,12 @@ WS.prototype.doOpen = function () {
107
111
}
108
112
109
113
try {
110
- this . ws = this . usingBrowserWebSocket && ! this . isReactNative ? ( protocols ? new WebSocket ( uri , protocols ) : new WebSocket ( uri ) ) : new WebSocket ( uri , protocols , opts ) ;
114
+ this . ws =
115
+ this . usingBrowserWebSocket && ! this . isReactNative
116
+ ? protocols
117
+ ? new WebSocketImpl ( uri , protocols )
118
+ : new WebSocketImpl ( uri )
119
+ : new WebSocketImpl ( uri , protocols , opts ) ;
111
120
} catch ( err ) {
112
121
return this . emit ( 'error' , err ) ;
113
122
}
@@ -280,5 +289,5 @@ WS.prototype.uri = function () {
280
289
*/
281
290
282
291
WS . prototype . check = function ( ) {
283
- return ! ! WebSocket && ! ( '__initialize' in WebSocket && this . name === WS . prototype . name ) ;
292
+ return ! ! WebSocketImpl && ! ( '__initialize' in WebSocketImpl && this . name === WS . prototype . name ) ;
284
293
} ;
0 commit comments