Skip to content

Commit 9d7dd6b

Browse files
authored
Proxy WebSocket connections and handle errors (#872)
1 parent 791aff1 commit 9d7dd6b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/http-server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ function HttpServer(options) {
182182
if (options.timeout !== undefined) {
183183
this.server.setTimeout(options.timeout);
184184
}
185+
186+
if (typeof options.proxy === 'string') {
187+
this.server.on('upgrade', function (request, socket, head) {
188+
proxy.ws(request, socket, head, {
189+
target: options.proxy,
190+
changeOrigin: true
191+
}, function (err, req, res) {
192+
if (options.logFn) {
193+
options.logFn(req, res, {
194+
message: err?.message,
195+
status: res?.statusCode });
196+
}
197+
res.emit('next');
198+
});
199+
});
200+
}
185201
}
186202

187203
HttpServer.prototype.listen = function () {

0 commit comments

Comments
 (0)