我一直无法找到任何文档/我的需求的答案。http-proxy-rules和Websockets
我在一个泡菜的东西。我正在开发一个websocket应用程序,它将允许通过创建新服务(websocket服务器)来扩展模块。当然这意味着需要连接更多的端口。问题是,我们的公司政策只有很少的端口打开,所以我需要代理我的请求。
我读过很多回答说使用NGINX,但我根本不能。首先,我正在运行Windows,其次我们公司对可以使用和不能使用的内容非常严格。不过,我可以安装任何节点模块。我试图将http-proxy模块和http-proxy-rules一起使用。
问题是,我得到404的每个websocket请求。我会注意到,默认代理(对于普通的web服务,而不是套接字)正在100%正常工作。
这里是我当前的代码:
var http = require('http'),
httpProxy = require('http-proxy'),
HttpProxyRules = require('http-proxy-rules');
// Set up proxy rules instance
var proxyRules = new HttpProxyRules({
rules: {
'.*/ws/admin': 'http://localhost:26266', // Rule for websocket service (admin module)
'.*/ws/quickquery': 'http://localhost:26265' // Rule for websocket service (quickquery module)
},
default: 'http://Surface.levisinger.com:8080' // default target
});
// Create reverse proxy instance
var proxy = httpProxy.createProxy();
// Create http server that leverages reverse proxy instance
// and proxy rules to proxy requests to different targets
http.createServer(function(req, res) {
// a match method is exposed on the proxy rules instance
// to test a request to see if it matches against one of the specified rules
var target = proxyRules.match(req);
if (target) {
//console.log(req);
console.log("Returning " + target + " for " + req.headers.host);
return proxy.web(req, res, {
target: target,
ws: true
});
}
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('The request url and path did not match any of the listed rules!');
}).listen(5050);
我连接到的WebSocket客户端代码如下所示:
var servPath = (cliSettings["AppPaths"]["Admin"] == null) ? 'http://' + window.location.hostname + ':5050' : cliSettings["AppPaths"]["Admin"],
AdminIO = new io(servPath, {
extraHeaders: {
Service: "Admin"
},
path: '/ws/admin'})
...
和WebSocket的服务器被称为是这样的:
io = require('socket.io').listen(26266,{ path: '/ws/admin'}) // can use up to 26484
我真的很希望这里有人会有一个想法。谢谢!