两个用户在 socket.io 中获得控制访问权限 - 竞争条件
Two user getting control access in socket.io - race condition
两个或更多用户收到可用房间的通知。
我是如何尝试阻止它的,并让它一次只通知一个用户可用。
socket.on('Check', function (room) {
io.in(room).clients((error, clients) => {
var hasPush = false;
clients.forEach(function (clientId) {
var client = io.sockets.connected[clientId];
if (client.hasPush) {
hasPush = true;
socket.emit('busy', room);
return;
}
}, this);
if (!hasPush) {
socket.hasPush = true;
socket.emit('available', room);
}
});
});
我试图阻止使用共享变量,但没有成功。
rooms = {};
socket.on('check', function (room) {
if (!rooms[room]) {
rooms[room] = true;
}
else {
socket.emit('busy', room);
return;
}
.......
rooms[room] = false;
}
现在我已经添加了两台带有 nginx 和 redis 的服务器,redis-lock 为我工作。
两个或更多用户收到可用房间的通知。
我是如何尝试阻止它的,并让它一次只通知一个用户可用。
socket.on('Check', function (room) {
io.in(room).clients((error, clients) => {
var hasPush = false;
clients.forEach(function (clientId) {
var client = io.sockets.connected[clientId];
if (client.hasPush) {
hasPush = true;
socket.emit('busy', room);
return;
}
}, this);
if (!hasPush) {
socket.hasPush = true;
socket.emit('available', room);
}
});
});
我试图阻止使用共享变量,但没有成功。
rooms = {};
socket.on('check', function (room) {
if (!rooms[room]) {
rooms[room] = true;
}
else {
socket.emit('busy', room);
return;
}
.......
rooms[room] = false;
}
现在我已经添加了两台带有 nginx 和 redis 的服务器,redis-lock 为我工作。