@@ -13,6 +13,7 @@ module.exports = HttpProxyMiddleware;
13
13
function HttpProxyMiddleware ( context , opts ) {
14
14
// https://github.com/chimurai/http-proxy-middleware/issues/57
15
15
var wsUpgradeDebounced = _ . debounce ( handleUpgrade ) ;
16
+ var wsInitialized = false ;
16
17
var config = configFactory . createConfig ( context , opts ) ;
17
18
var proxyOptions = config . options ;
18
19
@@ -43,15 +44,24 @@ function HttpProxyMiddleware(context, opts) {
43
44
}
44
45
45
46
if ( proxyOptions . ws === true ) {
47
+ // use initial request to access the server object to subscribe to http upgrade event
46
48
catchUpgradeRequest ( req . connection . server ) ;
47
49
}
48
50
}
49
51
50
52
function catchUpgradeRequest ( server ) {
51
- server . on ( 'upgrade' , wsUpgradeDebounced ) ;
53
+ // subscribe once; don't subscribe on every request...
54
+ // https://github.com/chimurai/http-proxy-middleware/issues/113
55
+ if ( ! wsInitialized ) {
56
+ server . on ( 'upgrade' , wsUpgradeDebounced ) ;
57
+ wsInitialized = true ;
58
+ }
52
59
}
53
60
54
61
function handleUpgrade ( req , socket , head ) {
62
+ // set to initialized when used externally
63
+ wsInitialized = true ;
64
+
55
65
if ( shouldProxy ( config . context , req ) ) {
56
66
var activeProxyOptions = prepareProxyRequest ( req ) ;
57
67
proxy . ws ( req , socket , head , activeProxyOptions ) ;
0 commit comments