简体   繁体   中英

I'm trying to hide a button from other sockets in node.js. After the buttons is clicked my a player i don't want other players to be able to click it

i need to be able to remove the buttons from the DOM when a player clicks a seat. this way two players cannot take the same seat. all buttons should be hidden on the client side after a player sits down so they cannot move seats.

server side

   socket.on('seat1',function(){
       player.x = 380
       player.y = 300
       player.number = 1
       console.log(player)

    socket.emit('seat1',function(){
        hideSeatButtons();
    })

   });

     socket.on('seat2',function(){
       player.x = 380
       player.y = 100
       player.number = 2
       console.log(player)


   });

client side

   var seat1 = function(){
        socket.emit('seat1',{

        });

   }

    var seat2 = function(){
        socket.emit('seat2',{

        });

   }


  var hideSeatButtons = function(){
        document.getElementById("seat1").style.display = "none";
        document.getElementById("seat2").style.display = "none";
   }

Assuming you have everything set up properly, you will need to listen on the client side for the event you are emitting from the server. Rather than passing a function when emitting, try passing a value or an object (I changed the name of the event being sent to the client to avoid confusion with the event intended for the server):

socket.on('seat1',function(){
   player.x = 380
   player.y = 300
   player.number = 1
   console.log(player)

   socket.emit('hideSeats', {
       seatIndex: 1,
       playerNumber: player.number
   });
});

On the client you can listen for 'hideSeats' and call your client code function from there.

socket.on('hideSeats', function(obj) {
    if (obj.playerNumber === player.number) {
        hideSeatButtons();
    } else {
        hideSingleSeatButton(obj.seatIndex);
    }
});

This assumes you write a function to hide a single seat -which is what I think you would want to do for any player who is not the one who chose a seat. For the player who chose a seat, you hide all seats.

Does this approach cover your use case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM