@@ -8,14 +8,15 @@ var parseqs = require('parseqs');
88var inherit = require ( 'component-inherit' ) ;
99var yeast = require ( 'yeast' ) ;
1010var debug = require ( 'debug' ) ( 'engine.io-client:websocket' ) ;
11+ var BrowserWebSocket = global . WebSocket || global . MozWebSocket ;
1112
1213/**
1314 * Get either the `WebSocket` or `MozWebSocket` globals
1415 * in the browser or the WebSocket-compatible interface
1516 * exposed by `ws` for Node environment.
1617 */
1718
18- var WebSocket = typeof window !== 'undefined' ? ( window . WebSocket || window . MozWebSocket ) : require ( 'ws' ) ;
19+ var WebSocket = BrowserWebSocket || ( typeof window !== 'undefined' ? null : require ( 'ws' ) ) ;
1920
2021/**
2122 * Module exports.
@@ -91,7 +92,7 @@ WS.prototype.doOpen = function(){
9192 opts . headers = this . extraHeaders ;
9293 }
9394
94- this . ws = new WebSocket ( uri , protocols , opts ) ;
95+ this . ws = BrowserWebSocket ? new WebSocket ( uri ) : new WebSocket ( uri , protocols , opts ) ;
9596
9697 if ( this . ws . binaryType === undefined ) {
9798 this . supportsBinary = false ;
@@ -158,15 +159,13 @@ WS.prototype.write = function(packets){
158159 var self = this ;
159160 this . writable = false ;
160161
161- var isBrowserWebSocket = global . WebSocket && this . ws instanceof global . WebSocket ;
162-
163162 // encodePacket efficient as it uses WS framing
164163 // no need for encodePayload
165164 var total = packets . length ;
166165 for ( var i = 0 , l = total ; i < l ; i ++ ) {
167166 ( function ( packet ) {
168167 parser . encodePacket ( packet , self . supportsBinary , function ( data ) {
169- if ( ! isBrowserWebSocket ) {
168+ if ( ! BrowserWebSocket ) {
170169 // always create a new object (GH-437)
171170 var opts = { } ;
172171 if ( packet . options ) {
@@ -185,7 +184,7 @@ WS.prototype.write = function(packets){
185184 //have a chance of informing us about it yet, in that case send will
186185 //throw an error
187186 try {
188- if ( isBrowserWebSocket ) {
187+ if ( BrowserWebSocket ) {
189188 // TypeError is thrown when passing the second argument on Safari
190189 self . ws . send ( data ) ;
191190 } else {
0 commit comments