javascript - Socket.io in express receives undefined data from the client -


i'm working on simple tic-tac-toe app , whenever player hits button trigger function markthecell(button) passing button's id. function emits player string value , button id server. put console log in client page see whether valid value being passed , is. when tried log in server value of player become object object , button id undefined. don't know if failed how put , use parameters correctly. hope lend because i'm stuck this.

index.html

<body>         <div class="col-lg-4"></div>         <div class="col-lg-4 jumbotron">             <h4>you player <span id="name"></span></h4>             <button id="one" onclick="markthecell(this.id)" class="btn btn-default"></button>             <button class="btn btn-default"></button>             <button class="btn btn-default"></button></br>             <button class="btn btn-default"></button>             <button class="btn btn-default"></button>             <button class="btn btn-default"></button></br>             <button class="btn btn-default"></button>             <button class="btn btn-default"></button>             <button class="btn btn-default"></button>         </div>         <div class="col-lg-4"></div>     </div>      <script src="/socket.io/socket.io.js"></script>     <script>         var socket = io();         var player = "";         $(document).ready(function () {              socket.on('player', function (playerno) {                 player = playerno;                 $('#name').text(playerno);             });              socket.on('hitamark', function (player, button) {                 console.log("in client hit mark player "+ player);                  console.log("in client hit mark button "+ button);                 $(button).text(player);             });         })         function markthecell(button) {             socket.emit('mark', {player,button });              console.log("in markthecell client hit mark player "+ player);                  console.log("in markthecell client hit mark button "+ button);         }     </script> </body> 

index.js

var player = 0; io.on('connection', function(socket){     if(player==0){         player++;         socket.emit('player', "x");         console.log("player "+ player +" connected");     }else if(player==1){          player++;         socket.emit('player', "o");         console.log("player "+ player +" connected");     }      socket.on('disconnect', function(){         console.log('a user disconnected');     });       socket.on('mark', function(player, button){         console.log("in server hit mark button "+ player);           console.log("in server hit mark player "+button);          io.emit('hitamark', {player,button});     }); });   http.listen(3000, function(){     console.log('server listening on port 3000!'); }); 

the screenshot of console in browser , cmd: enter image description here

enter image description here

try emitting {player:player,button:button} instead of {player,button}. may can work.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -