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
是的,只有一臺服務器。 –