node-http-proxy
node-http-proxy copied to clipboard
Is websocket proxying working?
trafficstars
I'm trying to run a basic example but getting an error:
TypeError: socket.setNoDelay is not a function
at Object.common.setupSocket (/home/ubuntu/http-proxy/build/webpack:/proxy-app/node_modules/http-proxy/lib/http-proxy/common.js:127:1)
The code:
const proxy = httpProxy.createProxyServer({
ws: true
});
proxy.on('upgrade', (req, socket, head) => {
const { query } = Url.parse(req.url, true);
const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
console.log('[WS] Redirecting (upgrade)...', { url: req.url, redirectParams });
proxy.ws(req, socket, head, redirectParams);
});
const server = http.createServer(async (req, res) => {
const { query } = Url.parse(req.url, true);
const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
console.log('[HTTP] Redirecting...', { url: req.url, redirectParams });
proxy.ws(req, res, redirectParams);
}
server.listen(80);
up
It does not seem to work for me either!
I'm trying to run a basic example but getting an error:
TypeError: socket.setNoDelay is not a function at Object.common.setupSocket (/home/ubuntu/http-proxy/build/webpack:/proxy-app/node_modules/http-proxy/lib/http-proxy/common.js:127:1)The code:
const proxy = httpProxy.createProxyServer({ ws: true }); proxy.on('upgrade', (req, socket, head) => { const { query } = Url.parse(req.url, true); const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } }; console.log('[WS] Redirecting (upgrade)...', { url: req.url, redirectParams }); proxy.ws(req, socket, head, redirectParams); }); const server = http.createServer(async (req, res) => { const { query } = Url.parse(req.url, true); const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } }; console.log('[HTTP] Redirecting...', { url: req.url, redirectParams }); proxy.ws(req, res, redirectParams); } server.listen(80);
@bushev but I think you need to do server.on('upgrade', ...
Add an error handler to the socket seems to help.
proxy.on('upgrade', (req, socket, head) => {
socket.on('error', (error) => {
console.error(error);
});
const { query } = Url.parse(req.url, true);
const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
console.log('[WS] Redirecting (upgrade)...', { url: req.url, redirectParams });
proxy.ws(req, socket, head, redirectParams)
});
When I use websocket config ,I meet the following error.
Invalid frame header

const proxyServer = httpProxy.createProxyServer({
secure: false, ws: true, toProxy: true, logLevel: 'debug', changeOrigin: false, hostRewrite: true
});
server.on('upgrade', function (req, socket, head) {
if (req.baseUrl === '/tty') {
const proxyKey = req.originalUrl.split('/')[2];
console.log('req.url', req.url);
socket.on('error', err => {
console.error(err); // ECONNRESET will be caught here
});
socket.on('data', data => {
});
codeServerProxy.ws(req, socket, head, {
target: httpAgent.url
});
}
});