@@ -38,7 +38,11 @@ export default class WebSocketChannel {
3838 * @param {ChannelConfig } config - configuration for this channel.
3939 * @param {function(): string } protocolSupplier - function that detects protocol of the web page. Should only be used in tests.
4040 */
41- constructor ( config , protocolSupplier = detectWebPageProtocol ) {
41+ constructor (
42+ config ,
43+ protocolSupplier = detectWebPageProtocol ,
44+ socketFactory = url => new WebSocket ( url )
45+ ) {
4246 this . _open = true
4347 this . _pending = [ ]
4448 this . _error = null
@@ -51,14 +55,14 @@ export default class WebSocketChannel {
5155 return
5256 }
5357
54- this . _ws = createWebSocket ( scheme , config . address )
58+ this . _ws = createWebSocket ( scheme , config . address , socketFactory )
5559 this . _ws . binaryType = 'arraybuffer'
5660
5761 let self = this
5862 // All connection errors are not sent to the error handler
5963 // we must also check for dirty close calls
6064 this . _ws . onclose = function ( e ) {
61- if ( ! e . wasClean ) {
65+ if ( e && ! e . wasClean ) {
6266 self . _handleConnectionError ( )
6367 }
6468 }
@@ -142,7 +146,7 @@ export default class WebSocketChannel {
142146 */
143147 close ( ) {
144148 return new Promise ( ( resolve , reject ) => {
145- if ( this . _ws && this . _ws . readyState != WS_CLOSED ) {
149+ if ( this . _ws && this . _ws . readyState !== WS_CLOSED ) {
146150 this . _open = false
147151 this . _clearConnectionTimeout ( )
148152 this . _ws . onclose = ( ) => resolve ( )
@@ -187,11 +191,11 @@ export default class WebSocketChannel {
187191 }
188192}
189193
190- function createWebSocket ( scheme , address ) {
194+ function createWebSocket ( scheme , address , socketFactory ) {
191195 const url = scheme + '://' + address . asHostPort ( )
192196
193197 try {
194- return new WebSocket ( url )
198+ return socketFactory ( url )
195199 } catch ( error ) {
196200 if ( isIPv6AddressIssueOnWindows ( error , address ) ) {
197201 // WebSocket in IE and Edge browsers on Windows do not support regular IPv6 address syntax because they contain ':'.
@@ -208,7 +212,7 @@ function createWebSocket (scheme, address) {
208212 // That is why here we "catch" SyntaxError and rewrite IPv6 address if needed.
209213
210214 const windowsFriendlyUrl = asWindowsFriendlyIPv6Address ( scheme , address )
211- return new WebSocket ( windowsFriendlyUrl )
215+ return socketFactory ( windowsFriendlyUrl )
212216 } else {
213217 throw error
214218 }
0 commit comments