简短描述 在NodeJS中使用群集 - > net.createServer - > Socket.IO,如何在发送给worker之前获取头文件或连接字符串?
详细说明 我有4个服务器,每个服务器运行8个CPU。使用负载均衡器和集群,我将获得32个节点+4个主节点。
负载均衡器将使用ip地址来确定它将使用的服务器。运行Unity Socket.IO否则我想使用JSESSIONID。当连接到达主节点时,它会在ip上查找以确定要使用的集群。
在这里,我真的想要使用其他东西,或者所有连接(或大多数)将在同一个群集上。所以使用连接字符串唯一ID或cookie。
连接时我在连接字符串中添加一个唯一值?unique = {guid}但是当到达主节点时,我无法获取此数据(或cookie /标题)。
// THIS IS MASTER NODE
var server = net.createServer({ pauseOnConnect: true }, function (connection) {
// HOW TO GET headers or URL from connection?
// Incoming request processing
var remote = connection.remoteAddress;
var local = connection.localAddress;
var cIp = remote + local;
var ip = cIp.match(/[0-9]+/g)[0].replace(/,/g, '');
var wIndex = ip % num_processes;
// here we get the worker
var worker = workers[wIndex];
logger.log("Message to work "+ worker+", remote: "+ remote+ ", local: "+ local+", ip: "+ ip +", index: "+ wIndex);
worker.send('sticky-session:connection', connection);
});
// THIS IS A WORKER NODE
process.on('message', function(message, connection) {
if (message !== 'sticky-session:connection') {
return;
}
// Emulate a connection event on the server by emitting the
// event with the connection the master sent us.
http.emit('connection', connection);
connection.resume();
});
我试图在“数据”中捕获信息。但随后聊天就停止了。
connection.on('data', function (data) {
/* WHERE I GET OUTPUT OF
GET /chat HTTP/1.1
Host: 127.0.0.1:5223
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,sv;q=0.6
*/
console.log(data.toString('utf8', 0, data.length));
// BUT THE CHAT WONT WORK
});