Node.js 节点http代理负载平衡websocket错误
我刚刚开始评估节点http代理,因为我需要一个可伸缩的web套接字服务器 我已经测试了存储库中提供的“带websockets的简单平衡器”示例,但当充当多个地址的代理时,它不起作用。它只能作为一个地址的代理 代理多个地址时,WebSocket挂起错误如下:Node.js 节点http代理负载平衡websocket错误,node.js,proxy,websocket,load-balancing,node-http-proxy,Node.js,Proxy,Websocket,Load Balancing,Node Http Proxy,我刚刚开始评估节点http代理,因为我需要一个可伸缩的web套接字服务器 我已经测试了存储库中提供的“带websockets的简单平衡器”示例,但当充当多个地址的代理时,它不起作用。它只能作为一个地址的代理 代理多个地址时,WebSocket挂起错误如下: Error: socket hang up at createHangUpError (http.js:1472:15) at Socket.socketOnEnd [as onend] (http.js:1568:23)
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
插座io 1.0.6
节点http代理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>
var socket=io();
socket.on('message',函数(数据){
控制台日志(数据);
});
使用WebSocket的节点http代理基本负载平衡测试
我认为这是一个基本的测试,但它不工作!有人能解释一下我做错了什么,并提出解决方案吗
非常感谢您的宝贵意见。Socket.io 1.0需要粘性会话。请参见socket.io/docs/using-multiple-nodes 首先engine.io发出xhr请求,然后发出websocket请求。两个请求都需要到达同一个socket.io服务器。如果engine.io需要回退到长轮询等,则更是如此
要修复它,您只需要让代理服务器会话感知。它仍然可以循环新的连接,但只要它提供socket.io请求,它就需要将后续请求从该会话路由到同一后端。socket.io 1.0需要粘性会话。请参阅,首先engine.io发出xhr请求,然后它发出websocket请求。两个请求都需要到达同一个socket.io服务器。感谢您的帮助。我现在理解了这个问题,希望我早一点检查过那个文档。如果你把你的评论作为答案重新发送,我会把它标记为最佳答案。