提问者:小点点

节点-HTTP-Proxy Socket.io websocket连接


我试图让node-http-proxy与socket.io一起工作,但它总是返回到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);

导致相同的错误。


共2个答案

匿名用户

用node-http-proxy代理websockets似乎在节点0.10上被破坏了。这里有一个尝试修复它的pull请求,但是请看我的评论:修复也不完全有效。

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

匿名用户

我目前使用的是(最新的1.0.1版)节点-HTTP-Proxy,我发现在节点0.10.20上进行了一次调整(见下文)之后,Websockets确实可以可靠地工作。默认情况下,node-http-proxy似乎支持xhr-polling,同时丢弃websocket连接。

在“examples”中,我添加了“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