我有一个使用集群的服务器,为了使它与socke.IO一起工作,我使用的是粘性会话,但是我的房间有问题(我不知道我的方式是否是最好的选择):集群正在实例化进程,每个进程都有特定数量的房间。
- Process 2
- Room1
- Room2
- Room N
我将一些用户连接到房间(只有一个进程)的方式是使用该路由,用户访问一个页面,当他试图与Socket.io建立连接时,我检查该URL,并使用该信息将他插入一个房间。
我的问题是用集群实现这个服务器,我不能在特定的房间中插入用户,因为有些房间只存在于特定的进程中,而粘性会话将用户放到另一个进程中。如何将用户放到另一个进程中的房间中?此外,使用只能看到的路线的进程,他是在服务器和我想显示每一个房间在页面上。
我已经读过关于redis的文章了,但是我没有找到使用Socket.io +集群(Sticky会话+redis适配器)+房间的github解决方案。
按照我的代码来分享我所做的事情:
//Cluster.Master with simplified Code
if (cluster.isMaster) {
var workers = [];
// Spawn workers.
for (var i = 0; i < num_processes; i++) {
spawn(i);
}
// Create the outside facing server listening on our port.
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 worker = workers[worker_index(connection.remoteAddress, num_processes)];
worker.send('sticky-session:connection', connection);
}).listen(process.env.PORT);
} else {
console.log('I am worker #' + cluster.worker.id);
var app = new express();
//view engine
app.set('views', './views');
app.set('view engine', 'pug');
//statics
app.use(express.static(path.join(__dirname, 'public')));
//rooms
app.use('/', rooms);
var server = app.listen(0, 'localhost'),
io = sio(server);
io.adapter(sio_redis({ host: 'localhost', port: 6379 }));
//This File has the socket events (socket.on('messageX', function(){}))
// And there I am
var realtime = require('./realtime/socketIOEvents.js')(io);
// Listen to messages sent from the master. Ignore everything else.
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.
server.emit('connection', connection);
connection.resume();
});
}发布于 2016-10-26 20:47:05
那是一件正确的事。每个sserver/进程都侦听redis队列中的主题。简而言之,socketio只是将事件发布到集群中的所有其他服务器/进程。因此,就房间而言,它们只是一组对在房间中听消息感兴趣的套接字的抽象。
即使套接字被分发到不同的服务器/进程,它们也可以是同一房间的一部分。当每条消息传入时,每个服务器都知道并传递给所需的套接字。
就正确性而言,您的体系结构也是正确的,因为您的代理决定也有选择地转发消息,但是它正在增加消息生命周期中的跳数。您确实不需要这个代理来处理套接字路由。
发布于 2016-10-25 06:09:58
下面是示例代码,它将基于与之连接的房间进行工作。
http://www.html5gamedevs.com/topic/12321-create-a-cluster-server-with-nodejs-and-socketio/
如果你在这篇文章中有问题,请告诉我。
发布于 2016-10-25 17:18:14
经过多次尝试,我在工作人员顶部的代理服务器和在另一台服务器(Redis)中注册房间的每个工作人员中都这样做。当我用房间的URL连接到服务器时,我的代理服务器检测到我试图连接的工作人员,并将我的web套接字连接路由到正确的工作人员。
Proxy
/ | \
worker(5rooms) worker(5rooms) worker(5rooms)
\ | /
Redis这解决了我的问题,但它没有使用socket.io-redis.我们如何用socket.io-redis解决这个问题?在redis中添加每个用户、套接字和变量才能使工作人员中的处理有效吗?
https://stackoverflow.com/questions/40074665
复制相似问题