Я создал простую многопользовательскую математическую игру в реальном времени в Meteor, которую вы можете попробовать здесь: http://mathplay.meteor.com
При локальной игре (используя разные браузеры) все работает нормально. Но когда я играю через интернет с друзьями, клиенты часто рассинхронизируются: вопрос, указанный как активный для одного игрока, на самом деле уже решен другим игроком.
Я предполагаю, что какой-то код, который должен быть предназначен только для сервера, вместо этого выполняется на одном из клиентов. Любые предложения о том, как отладить это поведение?
Вот что происходит на клиенте, когда пользователь отправляет ответ:
Template.number_input.events[okcancel_events('#answertextbox')] = make_okcancel_handler({
ok: function (text, event) {
question = Questions.findOne({ order_number: Session.get("current_question_order_number") });
if (question.answer == document.getElementById('answertextbox').value) {
console.log('True');
Questions.update(question._id, {$set: {text: question.text.substr(0, question.text.length - 1) + question.answer, player: Session.get("player_name")}});
callGetNewQuestion();
}
else {
console.log('False');
}
document.getElementById('answertextbox').value = "";
document.getElementById('answertextbox').focus();
}
});
callGetNewQuestion() вызывает это как на клиенте, так и на сервере:
getNewQuestion: function () {
var nr1 = Math.round(Math.random() * 100);
var nr2 = Math.round(Math.random() * 100);
question_string = nr1 + " + " + nr2 + " = ?";
question_answer = (nr1 + nr2);
current_order_number = Questions.find({}).count() + 1;
current_question_id = Questions.insert({ order_number: current_order_number, text: question_string, answer: question_answer });
return Questions.findOne({_id: current_question_id});//current_question_id;
},
Полный исходный код приведен здесь для справки: https://github. com/tomsoderlund/MathPlay