两个用户在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); } }); }); 

我试图阻止使用共享variables,但没有奏效。

 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为我工作。