gpt4 book ai didi

node.js - node-http-proxy socket.io websocket 连接

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:50 24 4
gpt4 key购买 nike

我正在尝试让 node-http-proxy 与 socket.io 一起工作,但它总是回落到 xhr,websockets 将无法工作,尽管我正在连接到 Node 中描述的升级事件- http 代理文档。

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);

导致同样的错误。

最佳答案

使用 node-http-proxy 代理 websockets 似乎在 Node 0.10 上被破坏了。这是一个试图修复它的拉取请求,但请参阅我的评论:该修复程序也没有完全起作用。

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

关于node.js - node-http-proxy socket.io websocket 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com