Score system for number of clicks, socket.io and Javascript -


i have application , counting clicks on object client side, sending server

   socket.emit('playerscore',score); 

pushing array server side

    socket.on('playerscore',function(data){      socket.score = data;     score.push(socket.score); 

then sending clients

    io.sockets.emit ('scores',score); 

but receives score once each client, , sends clients once each client. trying compare score sent client on server side , send highest number compare local score client has. i'm sending more once , filter doesn't seem work.

var scoredata = data;       var largest = scoredata[0]; (var = 0; < score.length; i++) {     if (largest < scoredata[i] ) {         largest = scoredata[i];     } } 

thank

i think should pass highest score (from server client) instead of entire array. highest score, client can check if has stored locally.

but maybe have better approach this:

  1. your array have:

    { socketid: '123123', score: 12 }

  2. when server receives score, push score , socketid

  3. get object array highest score

  4. pass clients

with this, client-side compare direcly id, instead of score.


Comments