node-http-proxy负载平衡websocket错误

时间:2014-07-21 19:04:54

标签: node.js proxy websocket load-balancing node-http-proxy

我刚开始评估node-http-proxy,因为我需要一个可扩展的Web套接字服务器。

我已经测试了存储库中提供的'simple-balancer-with-websockets'示例,但在充当多个地址的代理时它不起作用。它只能作为一个地址的代理!

当代理多个地址时,WebSocket挂起错误如下:

Error: socket hang up
    at createHangUpError (http.js:1472:15)
    at Socket.socketOnEnd [as onend] (http.js:1568:23)
    at Socket.g (events.js:180:16)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)

我正在使用:

节点0.10.26 socket io 1.0.6 node-http-proxy 1.1.5 平台OSX

以下是负载均衡器。它与提供的示例的唯一区别是使用的地址和监听端口。

var http = require('http'),
    httpProxy = require('http-proxy');

//
// A simple round-robin load balancing strategy.
// 
// First, list the servers you want to use in your rotation.
//
var addresses = [
    {
        host: 'localhost',
        port: 8000
    },
    {
        host: 'localhost',
        port: 8001
    },
    {
        host: 'localhost',
        port: 8002
    }
];

//
// Create a HttpProxy object for each target
//

var proxies = addresses.map(function (target) {

  return new httpProxy.createProxyServer({
    target: target
  });
});

//
// Get the proxy at the front of the array, put it at the end and return it
// If you want a fancier balancer, put your code here
//

function nextProxy() {
  var proxy = proxies.shift();
  proxies.push(proxy);
  return proxy;
}

// 
// Get the 'next' proxy and send the http request 
//

var server = http.createServer(function (req, res) {    
  nextProxy().web(req, res);
});

// 
// Get the 'next' proxy and send the upgrade request 
//

server.on('upgrade', function (req, socket, head) {
  nextProxy().ws(req, socket, head);
});

server.listen(9000);

充当上述负载均衡器目标的基本http服务器是:

var http = require('http'),
    fs = require('fs'),
    io = require('socket.io');

var args = process.argv.splice(2);

var port = args[0] || 8000;

server = http.createServer(function(req, res) {

    var filePath = (__dirname + '/public/connect.html');

    fs.readFile(filePath,function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });
});

server.listen(port, function() {

    console.log('ws listening on: ' + port);

});

io = io(server);

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

   console.log('socket connected');

    socket.emit('message', 'ws message from ' + port);
});

客户端html是:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io();

            socket.on('message', function (data) {

                console.log(data);
            });

    </script>

</head>
<body>
node-http-proxy basic load balance test with websockets
</body>
</html>

我认为这是一个基本测试,但它不起作用!任何人都可以解释我做错了什么并建议一个解决方案吗?

非常感谢您的任何想法。

1 个答案:

答案 0 :(得分:2)

Socket.io 1.0需要粘性会话。请参阅socket.io/docs/using-multiple-nodes

首先,engine.io发出xhr请求,然后发出一个websocket请求。这两个请求都需要到达相同的socket.io服务器。如果engine.io需要回退到长轮询等等,更是如此。 。

要修复它,您只需要识别代理服务器会话。它仍然可以循环新连接,但只要它服务于socket.io请求,它就需要将来自该会话的后续请求路由到同一个后端。