尝试使用net.createServer集群Socket.io。所有示例都使用IP来分割连接到女巫节点的连接。但是,我使用4台带有负载均衡器的服务器将ip指向不同的服务器。
因此,在节点集群中,我想使用唯一的id指向与特定集群的连接。 图中每个想要连接的用户都可以在连接URL中添加参数ws://localhost/socket.io?id = xxyyzz
如何在net.createServer
中获取连接网址今天的ip代码:
var server = net.createServer({ pauseOnConnect: true }, function(connection) {
// We received a connection and need to pass it to the appropriate
// worker. Get the worker for this connection's source IP and pass
// it the connection.
var remote = connection.remoteAddress;
var local = connection.localAddress;
var ip = (remote+local).match( /[0-9]+/g )[0].replace(/,/g, '');
var wIndex = ip % num_processes;
var worker = workers[wIndex];
worker.send('sticky-session:connection', connection);
});