【发布时间】:2015-11-22 11:57:30
【问题描述】:
所以我正在开发一个基于文本的游戏,用户将发布请求发送到用 node.js 编写的服务器。但是,我需要通过让第一个玩家在提交响应之前等待第二个玩家来匹配 2 个玩家。现在的问题是,在我调用一次发布请求后,我无法发送另一个发布请求以获得任何响应。似乎第一个线程正在阻止更多未来的请求。在使用回调和异步方面应该做什么。如果可能的话,我不想使用套接字,因为这会限制玩家可以编码的语言。
var newGame=0;
//joins new game (username,password)
app.post('/game/join', function(req, res) {
var username = req.body.username;
var password = req.body.password;
if(newGame==1){
res.send('Game Started');
newGame=0;
}
else{
newGame=1;
wait(username, function(){
res.send('Game Started'+username);
});
}
});
function wait(username){
while(newGame==1){
console.log(username + newGame);
}
}
【问题讨论】:
标签: javascript node.js multithreading post asynchronous