//----------BACKSTORY--------//
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.
The code used to work where server changes could be pushed on the fly and after a brief pause (during reconnection) the clients would continue displaying the new changes. This was great for when we were testing. Now I have to reset the server (automated through nodemon) then manually refresh each client.
//------QUESTION--------//
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
The act of writing this out led me to try something. If you notice in app.js and clientmain.js there is an interaction between requestPlayer and assignPlayer (emits / listeners). These were set to listen once (using socket .once method). Because of this the server .once was resetting (on server reset) but the client .once was not unless the page was refreshed. In the end I don't think the requestPlayer interaction is even necessary. I removed it and it works great.