@@ -348,7 +348,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
348
348
HttpProxy . prototype . proxyWebSocketRequest = function ( req , socket , head , buffer ) {
349
349
var self = this ,
350
350
outgoing = new ( this . target . base ) ,
351
- listeners = { } ,
352
351
errState = false ,
353
352
CRLF = '\r\n' ;
354
353
@@ -439,93 +438,17 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
439
438
// Any incoming data on this WebSocket to the proxy target
440
439
// will be written to the `reverseProxy` socket.
441
440
//
442
- proxySocket . on ( 'data' , listeners . onIncoming = function ( data ) {
443
- if ( reverseProxy . incoming . socket . writable ) {
444
- try {
445
- self . emit ( 'websocket:outgoing' , req , socket , head , data ) ;
446
- var flushed = reverseProxy . incoming . socket . write ( data ) ;
447
- if ( ! flushed ) {
448
- proxySocket . pause ( ) ;
449
- reverseProxy . incoming . socket . once ( 'drain' , function ( ) {
450
- try { proxySocket . resume ( ) }
451
- catch ( er ) { console . error ( "proxySocket.resume error: %s" , er . message ) }
452
- } ) ;
453
-
454
- //
455
- // Force the `drain` event in 100ms if it hasn't
456
- // happened on its own.
457
- //
458
- setTimeout ( function ( ) {
459
- reverseProxy . incoming . socket . emit ( 'drain' ) ;
460
- } , 100 ) ;
461
- }
462
- }
463
- catch ( ex ) {
464
- detach ( ) ;
465
- }
466
- }
467
- } ) ;
441
+ proxySocket . pipe ( reverseProxy . incoming . socket ) ;
468
442
469
443
//
470
444
// Any outgoing data on this Websocket from the proxy target
471
445
// will be written to the `proxySocket` socket.
472
446
//
473
- reverseProxy . incoming . socket . on ( 'data' , listeners . onOutgoing = function ( data ) {
474
- try {
475
- self . emit ( 'websocket:incoming' , reverseProxy , reverseProxy . incoming , head , data ) ;
476
- var flushed = proxySocket . write ( data ) ;
477
- if ( ! flushed ) {
478
- reverseProxy . incoming . socket . pause ( ) ;
479
- proxySocket . once ( 'drain' , function ( ) {
480
- try { reverseProxy . incoming . socket . resume ( ) }
481
- catch ( er ) { console . error ( "reverseProxy.incoming.socket.resume error: %s" , er . message ) }
482
- } ) ;
447
+ reverseProxy . incoming . socket . pipe ( proxySocket ) ;
483
448
484
- //
485
- // Force the `drain` event in 100ms if it hasn't
486
- // happened on its own.
487
- //
488
- setTimeout ( function ( ) {
489
- proxySocket . emit ( 'drain' ) ;
490
- } , 100 ) ;
491
- }
492
- }
493
- catch ( ex ) {
494
- detach ( ) ;
495
- }
496
- } ) ;
497
-
498
- //
499
- // Helper function to detach all event listeners
500
- // from `reverseProxy` and `proxySocket`.
501
- //
502
- function detach ( ) {
503
- proxySocket . destroySoon ( ) ;
504
- proxySocket . removeListener ( 'end' , listeners . onIncomingClose ) ;
505
- proxySocket . removeListener ( 'data' , listeners . onIncoming ) ;
506
- reverseProxy . incoming . socket . destroySoon ( ) ;
507
- reverseProxy . incoming . socket . removeListener ( 'end' , listeners . onOutgoingClose ) ;
508
- reverseProxy . incoming . socket . removeListener ( 'data' , listeners . onOutgoing ) ;
509
- }
510
-
511
- //
512
- // If the incoming `proxySocket` socket closes, then
513
- // detach all event listeners.
514
- //
515
- proxySocket . on ( 'end' , listeners . onIncomingClose = function ( ) {
516
- detach ( ) ;
517
-
518
- // Emit the `end` event now that we have completed proxying
449
+ proxySocket . on ( 'end' , function ( ) {
519
450
self . emit ( 'websocket:end' , req , socket , head ) ;
520
451
} ) ;
521
-
522
- //
523
- // If the `reverseProxy` socket closes, then detach all
524
- // event listeners.
525
- //
526
- reverseProxy . incoming . socket . on ( 'end' , listeners . onOutgoingClose = function ( ) {
527
- detach ( ) ;
528
- } ) ;
529
452
}
530
453
531
454
function getPort ( port ) {
0 commit comments