Skip to content

load balancer with web socket hangup error. #674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jonathanstone opened this issue Jul 20, 2014 · 2 comments
Closed

load balancer with web socket hangup error. #674

jonathanstone opened this issue Jul 20, 2014 · 2 comments

Comments

@jonathanstone
Copy link

Hi

I have just started evaluating http-proxy. I have tested the ’simple load balancer for websockets’ example and it fails with a WebSocket hangup error as follows:

Error: socket hang up
at createHangUpError (http.js:1472:15)
at Socket.socketOnEnd as onend
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)

I am using:

node 0.10.26
socket io 1.0.6
node-http-proxy 1.1.5

No errors occur if one proxy to address is used, but when mode than one address is used such as below, the socket hangup error occurs.

The following is the load balancer. Its only difference to the provided sample is the addresses used and the listen port.

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

The basic http server is:

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

});

If I reduce the addresses to one target then no errors occur.

Please could some help in identifying the cause of the error.

@jondubois
Copy link

See my response in this issue (seems to be the same problem): #527

@indexzero
Copy link
Member

Closing as a duplicate of #527

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants