2

我正在尝试使用 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);

导致相同的错误。

4

2 回答 2

4

使用 node-http-proxy 代理 websocket 似乎在 Node 0.10 上被破坏了。这是一个尝试修复它的拉取请求,但请参阅我的评论:修复也不完全有效。

https://github.com/nodejitsu/node-http-proxy/pull/402

于 2013-04-18T16:03:51.923 回答
2

我目前正在使用(最新的?v1.0.1)node-http-proxy,我发现 Websockets 在对 Node v0.10.20 进行调整(见下文)后确实可以可靠地工作。默认情况下,node-http-proxy 似乎支持 xhr-polling,同时丢弃 websocket 连接。

在“示例”中,我添加了“ws”标志 true

var http = require('http'),
    httpProxy = require('../../lib/http-proxy');

httpProxy.createServer({
  target:'http://somehost.tld:5000',
  ws : true //added Websockets Flag TRUE
}).listen(80);//arbitrary port
于 2014-01-26T12:25:36.820 回答