I have many websockify backends to which I get the address to from redis, so node is necessary. the problem is that I can’t get the Websocket connection to stay alive.
I am using express for the web server, and I tried using express-http-proxy, setting-up the proxy inside an “app.post” block after retrieving the address from redis and redirecting the user to it.
It almost works, but when the websockify webpage requests the websocket (wss://<my-domain>/novnc/websockify?token=userToken1:5901
) the connection is established but is closed immediately:
websockify log
I am very new to node so I’m pretty lost, any help is appreciated.
here’s the app.post code snippet:
app.post('/job', (req, res) => {
const job = req.query['jobID'];
const token = req.query['userToken'];
try {
redisGetItem(job).then(function(response) {
if (!response) { // check if the response is empty
console.error('key not found:', job);
return res.status(404).send('key not found');
}
var [addr, port] = response.split(':');
app.use('/websockify', proxy(`${addr}:${websockify_port}`, {https: true}))
app.use('/novnc', proxy(`${addr}:${websockify_port}`, {
https: true,
proxyReqPathResolver: (req) => {
return '/novnc'+req.url;
}, // websocket is instantly killed..
}));
res.redirect(`websockify/vnc.html?path=novnc/websockify?token=${token}:${port}`);
});
} catch (err) {
console.error("Error:", err);
res.status(500).send("error retrieving key:", err);
}
});
note: I implemented redisGetItem(key) and it works.