2
两位或两位以上用户会收到可用空间的通知。两位用户在socket.io中获取控制访问权限 - 竞争条件
我是如何防止它,并使其一次只有一个用户将被通知可用。
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;
}
这是只有一个服务器运行这个权利? – Legman
是的,只有一台服务器。 –