第一个playerHandler.nextTurn()将使用相应的套接字ID返回播放器列表中的第一个播放器。但是调用currsocket.emit将不会执行任何操作。然后我尝试了发送给所有其他玩家的io.emit。由于我最多有2个玩家,因此该事件不会在第一个玩家客户端套接字内触发。 调用第二个玩家currsocket.emit可以正常工作。 似乎setTimeout()存在一些问题,因为在我的其他代码中,这没有任何问题(没有超时)。
socket.on('add-player', (player) => {
//console.log('socket add player hits');
console.log("add user name :" + player.name);
//add the new player
var newPlayer = { 'id': socket.id.toString(), 'name': player.name };
//dont use this. for the var declared above
var canWeKeepHim = { value: false };
var package = playerHandler.methods.addNewPlayer(newPlayer, canWeKeepHim);
//on successful addition of new player add him to gameroom socket room
if (canWeKeepHim.value) {
socket.join('gameroom');
console.log(player.name + 'joined socket room');
}
socket.emit('add-player-message', { type: 'success-message', package: package });
io.emit('add-player-list', playerHandler.methods.getNewPlayer());
//is the async nature of socket events a problem , the start-game never seems to be true;
if (GameHandler.currentGameState == GameHandler.yetToStartGame && playerHandler.methods.doWeHaveMaxPlayers()) {
GameHandler.changeCurrentGameState(GameHandler.gameHasStarted);
var delay = 3000;
io.to('gameroom').emit('start-game', { value: true, delay: delay });
//if the game state is still gameHasStarted after delay then fire the gameplay events
GameHandler.gameArenaRedirectTimer = setTimeout(() => {
if (GameHandler.currentGameState == GameHandler.gameHasStarted) {
console.log('hey this works after 10s');
var currentPlayerTurn = playerHandler.methods.nextTurn();
currentPlayerTurn = playerHandler.methods.nextTurn();;
var currsocket = io.sockets.sockets[currentPlayerTurn.id];
currsocket.emit('next-turn', { myturn: true });
//console.log(io.sockets.so)
// io.emit('next-turn', { myturn: false, playerName: currentPlayerTurn.name });
//io.to('gameroom').emit('next-turn', { myturn: false, playerName: currentPlayerTurn.name });
}
}, delay);
}
});
答案 0 :(得分:0)
尝试将套接字传递给setTimout
GameHandler.gameArenaRedirectTimer = setTimeout((socket) => {
if (GameHandler.currentGameState == GameHandler.gameHasStarted) {
console.log('hey this works after 10s');
var currentPlayerTurn = playerHandler.methods.nextTurn();
currentPlayerTurn = playerHandler.methods.nextTurn();;
var currsocket = io.sockets.sockets[currentPlayerTurn.id];
currsocket.emit('next-turn', { myturn: true });
//console.log(io.sockets.so)
// io.emit('next-turn', { myturn: false, playerName: currentPlayerTurn.name });
//io.to('gameroom').emit('next-turn', { myturn: false, playerName: currentPlayerTurn.name });
}
}, delay,socket);
答案 1 :(得分:0)
我发现将setTimeout移出socket.on事件监听器回调并移出io.on事件监听器修复了问题。
function startCountDownToStartGame(callback, delay, arg) {
if (GameHandler.currentGameState == GameHandler.gameHasStarted) {
GameHandler.gameArenaRedirectTimer = setTimeout(callback, delay, arg);
}
};
io.on('connection', (socket) => {
console.log('user connected :' + socket.id);
socket.on('disconnect', function () {
console.log('user disconnected');
//TODO : remove player from the player list once hes disconnected
});
socket.on('add-player', (player) => {
//console.log('socket add player hits');
console.log("add user name :" + player.name);
//add the new player
var newPlayer = { 'id': socket.id.toString(), 'name': player.name };
//dont use this. for the var declared above
var canWeKeepHim = { value: false };
var package = playerHandler.methods.addNewPlayer(newPlayer, canWeKeepHim);
//on successful addition of new player add him to gameroom socket room
if (canWeKeepHim.value) {
socket.join('gameroom');
console.log(player.name + 'joined socket room');
}
socket.emit('add-player-message', { type: 'success-message', package: package });
io.emit('add-player-list', playerHandler.methods.getNewPlayer());
if (GameHandler.currentGameState == GameHandler.yetToStartGame && playerHandler.methods.doWeHaveMaxPlayers()) {
GameHandler.changeCurrentGameState(GameHandler.gameHasStarted);
var delay = 3000;
io.to('gameroom').emit('start-game', { value: true, delay: delay });
delay += 1000;// this is a temp fix
//it seems calling the settimeout within the socket.on will somehow affect the emit functions for all other socket
//other than the current socket , by placing the settimeout outside , it fixed the problem .
startCountDownToStartGame((socket) => {
var currentPlayerTurn = playerHandler.methods.nextTurn();
var currsocket = io.sockets.sockets[currentPlayerTurn.id];
currsocket.emit('next-turn', { myturn: true });
currsocket.broadcast.to('gameroom').emit('next-turn', { myturn: false, playerName: currentPlayerTurn.name });
}, delay, socket);
}
});
}