node.js:将WebSocket代理到其他端口

node.js:将WebSocket代理到其他端口,node.js,websocket,socket.io,Node.js,Websocket,Socket.io,我已经在node.js中编写了http代理,在端口80上运行。我只需要将socket.io流量重定向到端口9090,将标准http流量重定向到8080上的Apache。这是我的代理代码: httpProxy = require('http-proxy'); httpProxy.createServer(function (req, res, proxy) { if (req.url.match(/socket.io/)) { proxy.proxyRequest(req

我已经在node.js中编写了http代理,在端口80上运行。我只需要将socket.io流量重定向到端口9090,将标准http流量重定向到8080上的Apache。这是我的代理代码:

httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {
    if (req.url.match(/socket.io/)) {
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 9090
        });
    } else {
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 8080
        });
    }
}).listen(80);
一切正常,但io.socket会退回到xhr轮询

http://localhost/client.htm    - falls back to xhr-polling

file:///C:/.../client9090.htm  - still uses websocket
socket.io应用程序在端口9090上运行,client.htm连接到80,client9090.htm直接连接到9090

看起来节点http代理使socket.io应用程序在xhr轮询模式下工作。 客户端是Chrome v.25

socket.io应用程序代码

var io = require('socket.io').listen(9090);

io.on('connection', function (socket) {

        socket.on('hi!', function (data) {
            console.log(data);
            socket.emit('news');
        });

        socket.on('ahoj', function (data) {
            console.log(data);
        });
    });
client.htm代码

<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
    var chat = io.connect('http://localhost')

    chat.on('connect', function () {
        chat.emit('hi!');
    });

    chat.on('news', function () {
        chat.emit('ahoj',{a:1,b:2});
    });
</script>

var chat=io.connect('http://localhost')
chat.on('connect',函数(){
chat.emit('hi!');
});
chat.on('news',function(){
emit('ahoj',{a:1,b:2});
});
client9090.htm相同,但localhost替换为localhost:9090

正如我所说的,一切都很好,唯一的问题是,http代理使节点从WebSocket退回到xhr轮询。 有人能帮忙吗?

根据,在向httpProxy.createServer()添加回调时,您必须手动代理“升级”事件,因此类似以下内容:

httpProxy = require('http-proxy');

// added `var server =` here
var server = httpProxy.createServer(function (req, res, proxy) {
    if (req.url.match(/socket.io/)) {
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 9090
        });
    } else {
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 8080
        });
    }
}).listen(80);

// added upgrade listener section here:
server.on('upgrade', function (req, socket, head) {
    server.proxy.proxyWebSocketRequest(req, socket, head);
});
httpProxy = require('http-proxy');

var options = {
  pathnameOnly: true,
  router: {
    '/wiki': '127.0.0.1:8001',
    '/blog': '127.0.0.1:8002',
    '/api':  '127.0.0.1:8003'
  }
}

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(80);
但是,对于上面描述的用法,您甚至不需要回调函数-您也可以轻松地执行以下操作:

httpProxy = require('http-proxy');

// added `var server =` here
var server = httpProxy.createServer(function (req, res, proxy) {
    if (req.url.match(/socket.io/)) {
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 9090
        });
    } else {
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 8080
        });
    }
}).listen(80);

// added upgrade listener section here:
server.on('upgrade', function (req, socket, head) {
    server.proxy.proxyWebSocketRequest(req, socket, head);
});
httpProxy = require('http-proxy');

var options = {
  pathnameOnly: true,
  router: {
    '/wiki': '127.0.0.1:8001',
    '/blog': '127.0.0.1:8002',
    '/api':  '127.0.0.1:8003'
  }
}

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(80);