我在 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