Reputation: 569
I'm sending some data from my web page using socket.io and then handling it in the node.js back end.
The code I use to send my data is:
var joystick1 = new VirtualJoystick({
container : document.body,
strokeStyle : 'cyan'
});
setInterval(function(){
var outputEl = document.getElementById('resulty');
outputEl.innerHTML = joystick1.deltaY();
//THE DATA IS SENT HERE
socket.emit('key', {Speed: joystick1.deltaY()});
}, 1/30 * 1000);
joystick1.addEventListener('touchStartValidation', function(event){
var touch = event.changedTouches[0];
if( touch.pageX >= window.innerWidth/2 ) return false;
return true
});
I handle it in node.js using this code:
socket.on('key', KeyReceived); //Key received from client
function KeyReceived(data)
{
//DATA IS PRINTED HERE
console.log(data);
switch(data.Key)
{
case "Steering":
_direction = 90 + data.value;
console.log(_direction);
break;
case "Speed":
_speed = 90 + data.value;
console.log(_speed);
break;
case "Adjustment":
_adjustment = data.value;
break;
}
}
In the console this prints out my data, but for some reason it won’t go into my case statements because my keys don't match?
What do I need to change to make this work?
Upvotes: 1
Views: 140
Reputation: 2598
The data parameter is a javascript Object
or javascript hash. This object doesn't have a Key
or a Value
property.
To test if this your object has a key use data.hasOwnproperty('Speed')
. If you want to get the value of this property use data['Speed']
or the equivalent dot-notation data.Speed
.
Upvotes: 2
Reputation: 23045
Log the object you are sending to see its content, it does not have Key
.
So what you really want to do is to send this object:
socket.emit('key', { Key: 'Speed', value: joystick1.deltaY() });
Upvotes: 2