JayjayG
JayjayG

Reputation: 1

Socket.io making duplicate when emitting

Heading

  1. Im emitting games from a .json file, it all works fine
  2. When i restart my js code, it emits the games and

        socket.on('getgames', function (data) {
    socket.emit('updategames', {games:games});
    });
    

This is supposed to make a div class="game-:gameid", it works fine until either theres a connection error or i restart my js code. Then it will make duplicate divs and emit it twice, i dont want duplicate.

Upvotes: 0

Views: 110

Answers (1)

Joakim Ericsson
Joakim Ericsson

Reputation: 4696

This problem will occur then you call on:

socket.on('getgames', function (data) {
  socket.emit('updategames', {games:games});
});

multiple times. This will course socket.io to subscribe to your event 'getgames' multiple times.

My guess is that you call this code every time you restart your game. To fix it you either unsubscribe from the event or just call the code one time.

Upvotes: 0

Related Questions