확장 가능한 웹 소켓 서버가 있어야하므로 node-http-proxy를 평가하기 시작했습니다.node-http-proxy로드 균형 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 소켓 노드 HTTP 프록시 1.1.5 1.0.6 IO를 플랫폼 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>
나는 기본적인 검사로이를 생각하고 아직 작동하지 않습니다! 누구든지 내가 뭘 잘못하고 있는지 설명하고 해결책을 제발 제안 할 수 있습니까?
의견을 보내 주셔서 감사합니다.
Socket.io 1.0은 고정 세션이 필요합니다. http://socket.io/docs/using-multiple-nodes/를 참조하십시오. 먼저 engine.io가 xhr 요청을하고 websocket 요청을합니다. 두 요청 모두 동일한 socket.io 서버에 도달해야합니다. – generalhenry
도움에 감사드립니다. 지금 문제를 이해하고 이전에 해당 문서를 확인했으면 좋겠습니다. 답변으로 의견을 다시 보내 주시면 가장 좋은 답변으로 표시하겠습니다. – user3861900