【发布时间】:2017-02-25 17:53:19
【问题描述】:
我有一个使用集群的服务器,并使用 socke.IO 使其工作:集群正在实例化进程,每个进程都有特定数量的房间。
- 服务器
- 进程 1
- 一室
- 房间2
- N号房间
- 进程 2
- 一室
- 房间2
- N号房间
- 进程 1
我将一些用户连接到房间(只有一个进程)的方式是使用路由,用户在该路由中访问页面,当他尝试与 Socket.io 建立连接时,我会检查 URL 并使用它信息我将他插入一个房间。
我的问题是使用集群实现此服务器我无法将用户插入特定房间,因为有些房间只存在于特定进程中,并且粘性会话将他置于另一个进程中。如何将用户放在另一个进程中的房间中?另外,用户只能看到他在服务器中的进程的路线,我想显示页面中的每个房间。
我已经阅读了 Redis-Adapter,但我没有在 github 上找到使用 Socket.io + Cluster(Sticky-session + redis-adapter) + rooms 的解决方案。
按照我的代码分享我所做的事情:
//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();
});
}
【问题讨论】:
-
socket.io的redis连接器就是为了解决这个问题而设计的。
-
你有更好的例子吗? SocketIO 和 socketio-redis 的文档很糟糕:/
-
我已经看过文档,但它对我没有帮助,因为 redis 示例只是在服务器之间共享事件......因为我有房间管理,我应该复制逻辑(用户加入房间X, userleft, 用户在每台服务器上发送了一条消息 X,Y,Z ) ?
-
如果您使用 socket.io 房间,那么您可以添加到房间并向房间发送消息,socket.io/redis 适配器会自动为您处理多服务器的事情。它在 redis 中存储每个用户是哪个服务器,并将房间列表保存在 redis 中,因此当您想向房间广播时,它会转到 redis,获取房间中的用户列表,然后告诉服务器为每个用户发送给他们。我想这一切都已经为你解决了。如果不是,那么您还没有描述为什么内置解决方案不是您想要或需要的。
标签: javascript node.js websocket socket.io node-cluster