@@ -32,6 +32,7 @@ const StreamIdStack = require('./stream-id-stack');
3232const OperationState = require ( './operation-state' ) ;
3333const promiseUtils = require ( './promise-utils' ) ;
3434const { ExecutionOptions } = require ( './execution-options' ) ;
35+ const { WebSocketWrapper } = require ( './websocket' ) ;
3536
3637/**
3738 * Represents a connection to a Cassandra node
@@ -171,30 +172,70 @@ class Connection extends events.EventEmitter {
171172 const self = this ;
172173 this . log ( 'info' , `Connecting to ${ this . endpointFriendlyName } ` ) ;
173174
174- if ( ! this . options . sslOptions ) {
175- this . netClient = new net . Socket ( { highWaterMark : this . options . socketOptions . coalescingThreshold } ) ;
176- this . netClient . connect ( this . port , this . address , function connectCallback ( ) {
177- self . log ( 'verbose' , `Socket connected to ${ self . endpointFriendlyName } ` ) ;
178- self . bindSocketListeners ( ) ;
179- self . startup ( callback ) ;
180- } ) ;
181- }
182- else {
183- // Use TLS
184- const sslOptions = utils . extend ( { rejectUnauthorized : false } , this . options . sslOptions ) ;
175+ if ( this . options . transport ) {
176+ if ( this . options . transport . toLowerCase ( ) === 'securewebsocket' ) {
177+ // Use secure WebSocket
178+ const options = utils . extend ( { rejectUnauthorized : false , transport : this . options . transport } ,
179+ this . options . webSocketOptions ) ;
180+
181+ if ( ! options . protocols ) {
182+ options . protocols = [ 'cql' ] ;
183+ }
184+
185+ this . netClient = new WebSocketWrapper ( options ) ;
186+
187+ this . netClient . connect ( this . port , this . address , function connectCallback ( ) {
188+ self . log ( 'verbose' , `Secure WebSocket to ${ self . endpointFriendlyName } ` ) ;
189+ self . bindSocketListeners ( ) ;
190+ self . startup ( callback ) ;
191+ } ) ;
192+ } else {
193+ // Use WebSocket
194+ const options = utils . extend ( {
195+ transport : this . options . transport ,
196+ highWaterMark : this . options . socketOptions . coalescingThreshold ,
197+ handshakeTimeout : this . options . socketOptions . connectTimeout ,
198+ } , this . options . webSocketOptions ) ;
199+
200+ if ( ! options . protocols ) {
201+ options . protocols = [ 'cql' ] ;
202+ }
185203
186- if ( this . options . sni ) {
187- sslOptions . servername = this . _serverName ;
204+ this . netClient = new WebSocketWrapper ( options ) ;
205+
206+ this . netClient . connect ( this . port , this . address , function connectCallback ( ) {
207+ self . log ( 'verbose' , `WebSocket connected to ${ self . endpointFriendlyName } ` ) ;
208+ self . bindSocketListeners ( ) ;
209+ self . startup ( callback ) ;
210+ } ) ;
188211 }
212+ } else {
213+ // Use Socket
214+ if ( ! this . options . sslOptions ) {
215+ this . netClient = new net . Socket ( { highWaterMark : this . options . socketOptions . coalescingThreshold } ) ;
216+
217+ this . netClient . connect ( this . port , this . address , function connectCallback ( ) {
218+ self . log ( 'verbose' , `Socket connected to ${ self . endpointFriendlyName } ` ) ;
219+ self . bindSocketListeners ( ) ;
220+ self . startup ( callback ) ;
221+ } ) ;
222+ } else {
223+ // Use Socket with TLS
224+ const sslOptions = utils . extend ( { rejectUnauthorized : false } , this . options . sslOptions ) ;
189225
190- this . netClient = tls . connect ( this . port , this . address , sslOptions , function tlsConnectCallback ( ) {
191- self . log ( 'verbose' , `Secure socket connected to ${ self . endpointFriendlyName } with protocol ${ self . netClient . getProtocol ( ) } ` ) ;
192- self . bindSocketListeners ( ) ;
193- self . startup ( callback ) ;
194- } ) ;
226+ if ( this . options . sni ) {
227+ sslOptions . servername = this . _serverName ;
228+ }
195229
196- // TLSSocket will validate for values from 512 to 16K (depending on the SSL protocol version)
197- this . netClient . setMaxSendFragment ( this . options . socketOptions . coalescingThreshold ) ;
230+ this . netClient = tls . connect ( this . port , this . address , sslOptions , function tlsConnectCallback ( ) {
231+ self . log ( 'verbose' , `Secure socket connected to ${ self . endpointFriendlyName } with protocol ${ self . netClient . getProtocol ( ) } ` ) ;
232+ self . bindSocketListeners ( ) ;
233+ self . startup ( callback ) ;
234+ } ) ;
235+
236+ // TLSSocket will validate for values from 512 to 16K (depending on the SSL protocol version)
237+ this . netClient . setMaxSendFragment ( this . options . socketOptions . coalescingThreshold ) ;
238+ }
198239 }
199240
200241 this . netClient . once ( 'error' , function socketError ( err ) {
0 commit comments