我正在尝试使用 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);
导致相同的错误。