我需要从客户端的服务器获取信息。
所以在服务器端,当客户端第一次连接时我得到了这个:
socket.on('adduser', function(username){
// misc code, where i set num_player and whatnot
socket.emit('confirmauth', socket.id, socket.num_player, function(data){
console.log(data)
});
// code
}
在客户端我得到了这个:
var current_player;
socket.on('confirmauth', function(id, username, num, callback) {
current_player = new Player(username,id, num);
console.log(current_player.id); // works
console.log(current_player.num); //works
callback('ok i got it');
});
console.log(current_player.id); //undefined
console.log(current_player.num); //undefined
我的问题是在套接字之外,播放器未定义。看来javascript在继续之前不会等待我的套接字打开来检索数据。
我尝试将 socket.on 包装在 $.when 完成时,但它不起作用。我尝试进行回调,但我想我可能不太明白它应该如何工作。因此,如果你们中有人愿意帮助我,我将不胜感激
谢谢您的回答。