N
N
Nikolay Erofeev2016-02-13 17:48:21
JavaScript
Nikolay Erofeev, 2016-02-13 17:48:21

How to send data from socket.io client?

Hello. I can not understand why the data from the client is not sent.
Server:

io.sockets.on('connection', function (client) {
    client.on('messageClient', function (data) {
      console.log(data.posX);
    });  
    client.emit('messageServer', function (data) {
      console.log(data);
    });  
});

customer
var socket = io.connect('http://46.101.151.182:3000');
  var sid = Math.round('');
  socket.on('connect', function () {

    console.log('Подключен!');

  });

  var ax, ay, acelX;

  window.ondevicemotion = function(e) {
    ax = e.accelerationIncludingGravity.x;
    ay = e.accelerationIncludingGravity.y;
    document.getElementById('info').innerHTML = ax+', '+ay;
  }


  socket.on('messageServer',function(data) {
    console.log(data);
  });

  socket.emit('messageClient',{posX:ax});

When the page loads, it shows undefined, and that's it. Further data is not received

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2016-02-14
@superD

When the page loads, it shows undefined, and that's it. Further data is not received

You are passing the value of the variable ax, and at the time of sending it is undefined, this shows. Try passing a string.
If you want a message to be sent to the ondevicemotion event, put
socket.emit('messageClient', { posX: ax });
inside the callback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question