From 2e7c633e522c69b8b2fa0849dd0ec05fd5e5d6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Gr=C3=BCnwald?= Date: Thu, 28 Jul 2022 16:11:24 +0200 Subject: [PATCH] Add proxying websocket connections closes !142 --- lib/http-server.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/http-server.js b/lib/http-server.js index dfe4c474..eb8ad6ef 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -139,9 +139,10 @@ function HttpServer(options) { handleError: typeof options.proxy !== 'string' })); + var proxy; if (typeof options.proxy === 'string') { var proxyOptions = options.proxyOptions || {}; - var proxy = httpProxy.createProxyServer(proxyOptions); + proxy = httpProxy.createProxyServer(proxyOptions); before.push(function (req, res) { proxy.web(req, res, { target: options.proxy, @@ -181,6 +182,15 @@ function HttpServer(options) { if (options.timeout !== undefined) { this.server.setTimeout(options.timeout); } + + if (typeof options.proxy === 'string') { + this.server.on('upgrade', function (request, socket, head) { + proxy.ws(request, socket, head, { + target: options.proxy, + changeOrigin: true + }); + }); + } } HttpServer.prototype.listen = function () {