【发布时间】:2013-03-21 14:13:06
【问题描述】:
我正在尝试使用 socket.io 让 node-http-proxy 工作,但它总是回退到 xhr,websockets 将无法工作,尽管我正在连接到 node- 中描述的升级事件http-proxy 文档。
var httpProxy = require ("http-proxy");
var server = httpProxy.createServer (function (req, res, proxy) {
var buffer = httpProxy.buffer (req);
proxy.proxyRequest (req, res, {
host : "localhost",
port : 8001,
buffer : buffer
});
});
server.listen (80);
server.on('upgrade', function (req, socket, head) {
server.proxy.proxyWebSocketRequest(req, socket, head, { host : "localhost", port : 8001 });
});
应用程序显然在 localhost:8001 上运行,如果我允许所有传输方法,它会正常工作,因为它使用 xhrs。如果我强制它使用 websockets,firefox 会说
Firefox can't establish a connection to the server at ws://localhost/socket.io/1/websocket/cNp5J80KAWkXqjE6OZOt. @ http://localhost/socket.io/socket.io.js:2371
只使用默认方法
httpProxy.createServer (8001, "localhost").listen (80);
导致同样的错误。
【问题讨论】:
-
你没有和我一样的代码...你能链接你找到这个代码的文档吗?同时,您可以在this answer 中查看我的代码,这对我有用。如果仅在一个域中使用它,则可以删除 switch 语句。
-
我从这里得到了代码:github.com/nodejitsu/node-http-proxy#proxying-websockets(我有一个实验遗留下来的缓冲线,我现在删除了它:/)使用你的代码我仍然有同样的问题。难道是版本出了问题?我正在运行 node-http-proxy 0.10.0、socket.io 0.9.13 和 node 0.10.0。
标签: node.js socket.io node-http-proxy