Sunday 30 August 2020

Why is only one entry being added to this list when using websockets?

I want to add players to a list called "players" once they join a game. Once they click a button it starts a function on the client side that emits "joinGame" which is received on the server side like so:socket.on("joinGame", (data) => { waitRoom++; if (waitRoom === 2) { let lobbyLoop = setInterval(() => { lobbyClock++; console.log(lobbyClock); if (waitRoom > 10 || lobbyClock > 3) { waitRoom = 0; lobbyClock = 0; socket.join(roomno); io.sockets.in(roomno).emit("connectToRoom", roomno); roomno++; clearInterval(lobbyLoop); } }, 1000); } }); This logic is for matchmaking. After two people join it starts a countdown and if the timer reaches 3 or if there are more than 10 players in the wait room it assigns them to a room number. Then it emits "connectToRoom" to the client:socket.on("connectToRoom", function (data) { roomno = data; socket.emit("addPlayer", { mousePosX, mousePosY, radius, playerID, roomno, }); }); This sends the room number to client which is stored in a local variable. Then it sends the players information to the server which is received here:socket.on("addPlayer", (data) => { players.push(data); console.log(players); }); The bug is that in "addPlayer" only one entry is present when I log it. I'm wondering if any of you can find out why this behavior is happening. I want players to fill up with everyone who was in the wait room.

Submitted August 30, 2020 at 10:13PM by TheSlothJesus

No comments:

Post a Comment