@@ -496,7 +496,7 @@ var WebSocket = function(url, proto, opts) {
496
496
if ( u . protocol === 'ws:' || u . protocol === 'wss:' ) {
497
497
protocol = u . protocol === 'ws:' ? http : https ;
498
498
port = u . protocol === 'ws:' ? 80 : 443 ;
499
- agent = u . protocol === 'ws:' ? protocol . getAgent ( u . hostname , u . port || port ) : protocol . getAgent ( {
499
+ agent = u . protocol === new protocol . Agent ( {
500
500
host : u . hostname ,
501
501
port : u . port || port
502
502
} ) ;
@@ -514,113 +514,111 @@ var WebSocket = function(url, proto, opts) {
514
514
throw new Error ( 'Invalid URL scheme \'' + urlScheme + '\' specified.' ) ;
515
515
}
516
516
517
- if ( ! agent . _events || agent . _events [ 'upgrade' ] . length === 0 ) {
518
- agent . on ( 'upgrade' , ( function ( ) {
519
- var data = undefined ;
520
-
521
- return function ( res , s , head ) {
522
- stream = s ;
523
-
524
- //
525
- // Emit the `wsupgrade` event to inspect the raw
526
- // arguments returned from the websocket request.
527
- //
528
- self . emit ( 'wsupgrade' , httpHeaders , res , s , head ) ;
517
+ var httpReq = protocol . request ( {
518
+ host : u . hostname ,
519
+ method : 'GET' ,
520
+ agent : agent ,
521
+ port : u . port ,
522
+ path : httpPath ,
523
+ headers : httpHeaders
524
+ } ) ;
525
+
526
+ httpReq . on ( 'error' , function ( e ) {
527
+ errorListener ( e ) ;
528
+ } ) ;
529
+
530
+ httpReq . on ( 'upgrade' , ( function ( ) {
531
+ var data = undefined ;
532
+
533
+ return function ( res , s , head ) {
534
+ stream = s ;
535
+
536
+ //
537
+ // Emit the `wsupgrade` event to inspect the raw
538
+ // arguments returned from the websocket request.
539
+ //
540
+ self . emit ( 'wsupgrade' , httpHeaders , res , s , head ) ;
541
+
542
+ stream . on ( 'data' , function ( d ) {
543
+ if ( d . length <= 0 ) {
544
+ return ;
545
+ }
529
546
530
- stream . on ( 'data' , function ( d ) {
531
- if ( d . length <= 0 ) {
532
- return ;
533
- }
534
-
535
- if ( ! data ) {
536
- data = d ;
537
- } else {
538
- var data2 = new buffer . Buffer ( data . length + d . length ) ;
547
+ if ( ! data ) {
548
+ data = d ;
549
+ } else {
550
+ var data2 = new buffer . Buffer ( data . length + d . length ) ;
539
551
540
- data . copy ( data2 , 0 , 0 , data . length ) ;
541
- d . copy ( data2 , data . length , 0 , d . length ) ;
552
+ data . copy ( data2 , 0 , 0 , data . length ) ;
553
+ d . copy ( data2 , data . length , 0 , d . length ) ;
542
554
543
- data = data2 ;
544
- }
555
+ data = data2 ;
556
+ }
545
557
546
- if ( data . length >= 16 ) {
547
- var expected = computeSecretKeySignature ( key1 , key2 , challenge ) ;
548
- var actual = data . slice ( 0 , 16 ) . toString ( 'binary' ) ;
558
+ if ( data . length >= 16 ) {
559
+ var expected = computeSecretKeySignature ( key1 , key2 , challenge ) ;
560
+ var actual = data . slice ( 0 , 16 ) . toString ( 'binary' ) ;
549
561
550
- // Handshaking fails; we're donezo
551
- if ( actual != expected ) {
552
- debug (
553
- 'expected=\'' + str2hex ( expected ) + '\'; ' +
554
- 'actual=\'' + str2hex ( actual ) + '\''
562
+ // Handshaking fails; we're donezo
563
+ if ( actual != expected ) {
564
+ debug (
565
+ 'expected=\'' + str2hex ( expected ) + '\'; ' +
566
+ 'actual=\'' + str2hex ( actual ) + '\''
567
+ ) ;
568
+
569
+ process . nextTick ( function ( ) {
570
+ // N.B. Emit 'wserror' here, as 'error' is a reserved word in the
571
+ // EventEmitter world, and gets thrown.
572
+ self . emit (
573
+ 'wserror' ,
574
+ new Error ( 'Invalid handshake from server:' +
575
+ 'expected \'' + str2hex ( expected ) + '\', ' +
576
+ 'actual \'' + str2hex ( actual ) + '\''
577
+ )
555
578
) ;
556
579
557
- process . nextTick ( function ( ) {
558
- // N.B. Emit 'wserror' here, as 'error' is a reserved word in the
559
- // EventEmitter world, and gets thrown.
560
- self . emit (
561
- 'wserror' ,
562
- new Error ( 'Invalid handshake from server:' +
563
- 'expected \'' + str2hex ( expected ) + '\', ' +
564
- 'actual \'' + str2hex ( actual ) + '\''
565
- )
566
- ) ;
567
-
568
- if ( self . onerror ) {
569
- self . onerror ( ) ;
570
- }
571
-
572
- finishClose ( ) ;
573
- } ) ;
574
- }
580
+ if ( self . onerror ) {
581
+ self . onerror ( ) ;
582
+ }
575
583
576
- //
577
- // Un-register our data handler and add the one to be used
578
- // for the normal, non-handshaking case. If we have extra
579
- // data left over, manually fire off the handler on
580
- // whatever remains.
581
- //
582
- stream . removeAllListeners ( 'data' ) ;
583
- stream . on ( 'data' , dataListener ) ;
584
+ finishClose ( ) ;
585
+ } ) ;
586
+ }
584
587
585
- readyState = OPEN ;
588
+ //
589
+ // Un-register our data handler and add the one to be used
590
+ // for the normal, non-handshaking case. If we have extra
591
+ // data left over, manually fire off the handler on
592
+ // whatever remains.
593
+ //
594
+ stream . removeAllListeners ( 'data' ) ;
595
+ stream . on ( 'data' , dataListener ) ;
586
596
587
- process . nextTick ( function ( ) {
588
- self . emit ( 'open' ) ;
597
+ readyState = OPEN ;
589
598
590
- if ( self . onopen ) {
591
- self . onopen ( ) ;
592
- }
593
- } ) ;
599
+ process . nextTick ( function ( ) {
600
+ self . emit ( 'open' ) ;
594
601
595
- // Consume any leftover data
596
- if ( data . length > 16 ) {
597
- stream . emit ( 'data' , data . slice ( 16 , data . length ) ) ;
602
+ if ( self . onopen ) {
603
+ self . onopen ( ) ;
598
604
}
605
+ } ) ;
606
+
607
+ // Consume any leftover data
608
+ if ( data . length > 16 ) {
609
+ stream . emit ( 'data' , data . slice ( 16 , data . length ) ) ;
599
610
}
600
- } ) ;
601
- stream . on ( 'fd' , fdListener ) ;
602
- stream . on ( 'error' , errorListener ) ;
603
- stream . on ( 'close' , function ( ) {
604
- errorListener ( new Error ( 'Stream closed unexpectedly.' ) ) ;
605
- } ) ;
606
-
607
- stream . emit ( 'data' , head ) ;
608
- } ;
609
- } ) ( ) ) ;
610
- }
611
-
612
- agent . on ( 'error' , function ( e ) {
613
- errorListener ( e ) ;
614
- } ) ;
611
+ }
612
+ } ) ;
613
+ stream . on ( 'fd' , fdListener ) ;
614
+ stream . on ( 'error' , errorListener ) ;
615
+ stream . on ( 'close' , function ( ) {
616
+ errorListener ( new Error ( 'Stream closed unexpectedly.' ) ) ;
617
+ } ) ;
615
618
616
- var httpReq = protocol . request ( {
617
- host : u . hostname ,
618
- method : 'GET' ,
619
- agent : agent ,
620
- port : u . port ,
621
- path : httpPath ,
622
- headers : httpHeaders
623
- } ) ;
619
+ stream . emit ( 'data' , head ) ;
620
+ } ;
621
+ } ) ( ) ) ;
624
622
625
623
httpReq . write ( challenge , 'binary' ) ;
626
624
httpReq . end ( ) ;
0 commit comments