Strange behavior Sockets Suspension NodeJS

I am getting this strange suspension of my sockets when disconnecting them. When I disconnect one of the connected sockets in the app it works fine, but when the initial socket/user is suspended the app is disconnecting another user/socket. I must say that the app is connecting some balls moving on an X and Y fashion and thus sharing their position in the canvas inside my P5JS sketch.

Here is how I am dealing with my connected/disconnected and data from the sockets/users.

io.sockets.on('connection', (socket) => { clients[socket.id] = socket console.log(socket.client.conn.server.clientsCount + " users connected " + socket.id) socket.on('new_ball', (data) => { let ball = new Ball(socket.id, data.x, data.y, data.r*2) balls.push(ball) }) socket.on('update', (data) => { let ball = {} for(let i=0; i < balls.length; i++) { if(socket.id == balls[i].id) { ball = balls[i] } } ball.x = data.x, ball.y = data.y, ball.r = data.r }) socket.on('disconnect', (cause) => { const index = balls.indexOf(socket.id) let usersCount = socket.client.conn.server.clientsCount try { balls.splice(index, 1) delete clients[socket.id] } catch (error) { } console.info("user disconnected reason: " + cause, "-", usersCount + " remained", balls.length) if(usersCount == 0){ console.log('waiting users to log in') } }) }) 

submitted by /u/Konvas
[link] [comments]

Tags: