2017-09-26 1 views
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; 
} 
+0

이 권한을 실행하는 서버는 하나입니까? – Legman

+0

예, 단 하나의 서버. –

답변

0

이제 두 개의 서버에 nginx 및 redis, redis-lock 작업이 추가되었습니다.