So I am working on a pong game with my friend. We built the game to work client side but after learning some backend we spent the weekend converting it to run server side and emit object data to the clients to be drawn client side.
It is currently structured to receive client requests for “player” assignment. When 2 players have been assigned the game will start (server side) and then begin emitting object data to the clients.
Everything works fine the first time users connect but here comes the issue. When I make a change server side the server resets (using nodemon but that isnt relevant). When the server resets both clients immediately try to reconnect and when they do they are both assigned as player 1 and therefore the game locks up until they refresh their pages.
My question is how do i allow the clients to reconnect without both being assigned as player 1 (and more importantly having the server only recognize the player count as 1 and therefore not reinitialize the game)?
I have tried putting delays (setTimeout) on player assignment but quickly realized how futile this is. Any delay is experienced by both clients during reconnect in the same time frame so it makes no difference whether they reconnect instantly or both at some delay in the future.
Below are links to the relevant branch files:
- ReadMe: This is a writeup I made that dictates the steps and interactions between server and client. This is the same way the code is structured
- App.js: Server file where the initial interactions between client / server take place
- ServerMain.js: this is the main server file. It is being used in app.js as ‘Main’. The game is run / emitted from this file
- ClientMain.js: this is the main client file served to each connected client. contains all interactions with the server
Please help! I would love to figure this out. Been trying and googling for days and haven’t had any luck.