我正在制作多人游戏。下面是我收到错误时的流程:
(1)登录后,用户名将存储在会话中:
socket.on('login', function(username)
{
socket.handshake.session.username = username;
socket.handshake.session.save();
var afterLogin = '/groups';
socket.emit('afterLogin', afterLogin);
});
(2)在完成游戏用户发送得分和房间ID到服务器后,用户重定向到另一个页面并完成游戏:
var submitScore = { score: hit, room: 'gryffindor'};
socket.emit('submitScore', submitScore);
(3)服务器获取分数和房间ID并遍历所有客户端并确定最大分数,从会话中获取用户名,并将获胜者姓名和分数发送给所有用户:
socket.on('submitScore', function(submitScore)
{
var room = submitScore.room;
var score = submitScore.score;
if (room == "gryffindor")
{
gryfScoreArray[socket.handshake.session.username] = score;
checkGryfUser --;
if (checkGryfUser == 0)
{
var max = 0;
var winnerName;
for(var index in gryfScoreArray)
{
if (gryfScoreArray[index] > max)
{
max = gryfScoreArray[index];
winnerName = index;;
}
}
// console.log(socket.id);
var winner = {username: winnerName, maxScore: max}
io.in(room).emit('gryfWinner', winner);
// console.log(winner.username);
// console.log(winner.maxScore);
}
}
所以,问题是socket.handshake.session.username
在点上未定义(3)