【发布时间】:2016-07-03 01:09:20
【问题描述】:
我想让答案验证仅在从客户端收到两个唯一响应后才触发。
var nrecieved = 0;
var responses = {}; // Socket id to response
function finish(){
// Loop through users in game and send them their responses
for(var id in responses){
if(responses.hasOwnProperty(id)){
// Send the response
io.to(id).emit('updatePlayer', responses[id]);
}
}
}
socket.on('playerCorrect', function (data) {
nrecieved++;
responses[socket.id] = data;
answerValidation(nrecieved);
});
socket.on('playerWrong', function (data) {
nrecieved++;
responses[socket.id] = data;
answerValidation(nrecieved);
});
console.log(nrecieved);
function answerValidation(value) {
nrecieved = value + value;
console.log(value);
if(nrecieved == 2){
finish();
dataRequest();
}
}
问题是我不能真正使用 nrecieved++;在那些套接字事件中,否则它不会正确计数。如果玩游戏的客户必须选择他们的答案并且会触发套接字事件然后 ++,有没有办法触发计数器?
【问题讨论】:
-
这似乎是您正在尝试做的事情: 1. 接收任何类型的响应(错误或正确) 2. 增加每个响应的接收计数器,无论哪种类型。 3. 当收到任何 2 个回复(错误或正确)时,向所有人发送回复?这是正确的还是我错过了什么?
-
你是对的。这只是检查是否有两个答案。每个玩家一个
-
每个玩家只有一个?
标签: javascript jquery socket.io