@@ -3,31 +3,10 @@ const parser = require("engine.io-parser");
33const parseqs = require ( "parseqs" ) ;
44const yeast = require ( "yeast" ) ;
55const { pick } = require ( "../util" ) ;
6+ const { WebSocket, usingBrowserWebSocket } = require ( "./websocket-constructor" ) ;
67
78const debug = require ( "debug" ) ( "engine.io-client:websocket" ) ;
89
9- let BrowserWebSocket , NodeWebSocket ;
10-
11- if ( typeof WebSocket !== "undefined" ) {
12- BrowserWebSocket = WebSocket ;
13- } else if ( typeof self !== "undefined" ) {
14- BrowserWebSocket = self . WebSocket || self . MozWebSocket ;
15- }
16-
17- if ( typeof window === "undefined" ) {
18- try {
19- NodeWebSocket = require ( "ws" ) ;
20- } catch ( e ) { }
21- }
22-
23- /**
24- * Get either the `WebSocket` or `MozWebSocket` globals
25- * in the browser or try to resolve WebSocket-compatible
26- * interface exposed by `ws` for Node-like environment.
27- */
28-
29- let WebSocketImpl = BrowserWebSocket || NodeWebSocket ;
30-
3110class WS extends Transport {
3211 /**
3312 * WebSocket transport constructor.
@@ -42,10 +21,6 @@ class WS extends Transport {
4221 if ( forceBase64 ) {
4322 this . supportsBinary = false ;
4423 }
45- this . usingBrowserWebSocket = BrowserWebSocket && ! opts . forceNode ;
46- if ( ! this . usingBrowserWebSocket ) {
47- WebSocketImpl = NodeWebSocket ;
48- }
4924 // WebSockets support binary
5025 this . supportsBinary = true ;
5126 }
@@ -91,11 +66,11 @@ class WS extends Transport {
9166
9267 try {
9368 this . ws =
94- this . usingBrowserWebSocket && ! this . opts . isReactNative
69+ usingBrowserWebSocket && ! this . opts . isReactNative
9570 ? protocols
96- ? new WebSocketImpl ( uri , protocols )
97- : new WebSocketImpl ( uri )
98- : new WebSocketImpl ( uri , protocols , opts ) ;
71+ ? new WebSocket ( uri , protocols )
72+ : new WebSocket ( uri )
73+ : new WebSocket ( uri , protocols , opts ) ;
9974 } catch ( err ) {
10075 return this . emit ( "error" , err ) ;
10176 }
@@ -156,7 +131,7 @@ class WS extends Transport {
156131 parser . encodePacket ( packet , self . supportsBinary , function ( data ) {
157132 // always create a new object (GH-437)
158133 const opts = { } ;
159- if ( ! self . usingBrowserWebSocket ) {
134+ if ( ! usingBrowserWebSocket ) {
160135 if ( packet . options ) {
161136 opts . compress = packet . options . compress ;
162137 }
@@ -176,7 +151,7 @@ class WS extends Transport {
176151 // have a chance of informing us about it yet, in that case send will
177152 // throw an error
178153 try {
179- if ( self . usingBrowserWebSocket ) {
154+ if ( usingBrowserWebSocket ) {
180155 // TypeError is thrown when passing the second argument on Safari
181156 self . ws . send ( data ) ;
182157 } else {
@@ -278,8 +253,8 @@ class WS extends Transport {
278253 */
279254 check ( ) {
280255 return (
281- ! ! WebSocketImpl &&
282- ! ( "__initialize" in WebSocketImpl && this . name === WS . prototype . name )
256+ ! ! WebSocket &&
257+ ! ( "__initialize" in WebSocket && this . name === WS . prototype . name )
283258 ) ;
284259 }
285260}
0 commit comments