[SOCKET.IO] issue with client reconnection after server reset

[SOCKET.IO] issue with client reconnection after server reset
0

#1

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.


#2

A secondary issue (that may end up helping) is that my listener for io.sockets.on(‘disconnect’) is not functioning properly. I think if I was able to capture the disconnects then it may be easier to distribute the correct player numbers.

Is ‘disconnect’ not a valid event? Ive tried putting the listener in and console logging which player disconnected but it never reached that callback.


#3

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.


#4

In general you should probably utilize the socket.id property that is a unique property given for each connection to the server.

let players = [];
function processConnection(socket) {
  players.push(socket.id);
}

#5

Thanks that’s a much better way to do it. Then I just use players.length to determine number of players.

Do you know if the event ‘disconnect’ is no longer valid? I need a way to track when a player either closes the tab or loses connection to the server.

I have a listener

sockets.io.on('disconnect', socket => console.log(`player ${players.length} has disconnected`);

but it doesnt seem to be working when i test closing a client tab.


#6

sorry full listener code:

io.sockets.on('disconnect', socket => console.log(`player ${players.length} has disconnected`);

players.splice(players.indexOf(socket.id), 1);