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

现在,我添加了两个服务器,并为我添加了redis-lock工作。

最新更新