【问题标题】:node-http-proxy socket.io websocket connectionnode-http-proxy socket.io websocket 连接
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 谢谢大卫...我想知道为什么我的应用程序无法在新的 node.js 安装上运行-.-
【解决方案2】:

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

在“examples”中,我添加了“ws”标志为真

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2014-09-12
    • 2014-03-04
    • 2012-05-06
    相关资源
    最近更新 更多