So I have a node.js server with socket.io which has a global variablegameStates The idea is this holds the info of all running games. This is updated via client emmits such as:const addMonster = (size, row, type) => { console.log(size,row,type); socket.emit('addMonster',roomId,size,row,type); } Which calls: socket.on('addMonster', (roomId, size,row,type) => { let health = (2**size)*25; let x = (gameStates[roomId].players[socket.id].number == 1) ? 0:400; let newMonster = { row:row, x:x, health:health, size:size, type:type, teamId:socket.id, target:null, }; if(!checkCollision(newMonster, roomId)){ gameStates[roomId].players[socket.id].monsters.push(newMonster); } }); On the client side.The game is in a game namespace: Server:let games = io.of('/game').on('connection', function(socket){ Client:var socket = io.connect('/game'); The issue is that when I try to send the game data to the user:const sendState = (game) =>{ console.log(gameStates[game]); games.to(game).emit('state', gameStates[game]); } It sends the gameStates initial state not the updated one from things such as addMonster and new players.This issue started when I moved to a multi room/namespace solution and was not a problem with just one room/namespace.There are no database requests which lowers the chance of an async problem.The send state is called 60 times a second by a setInterval method.Thanks for any help and if this is the wrong place to post please say and I will of course delete it.Thanks! Ed.
Submitted January 15, 2019 at 11:59PM by thelynched
No comments:
Post a Comment