0

I try to emit event "player move" with a json-object:

 this.broadcast.emit("player move", JSON.stringify(player)); 

but on the client I get this:

INFO: > 5:::{"args":[{"x":108,"y":105}],"name":"player move"} 

Which doesnt show an event. Why is this? (This is from a socket.io framework in JavaScript).

Im using socket.io 0.9.17

EDIT: More code

client.on('player move', onPlayerMove); 

Then the function:

function onPlayerMove(data) { var player = playerWithId(this.id); if (!player) { util.log("Player not found: " + this.id); return; } player.setX(data.x); player.setY(data.y); this.broadcast.emit("player move", JSON.stringify(player)); 

}

1 Answer 1

1

If I am not mistaking, you should have apostrophes at the first curly bracket and the one directly after the square bracket, which makes a difference between JSON objects and JavaScript objects. Example:

var text = '{ "employees" : [' + '{ "firstName":"John" , "lastName":"Doe" },' + '{ "firstName":"Anna" , "lastName":"Smith" },' + '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

Reference: JSON Howto - W3Schools

I hope this answers your question!

Sign up to request clarification or add additional context in comments.

2 Comments

Well I use JSON.stringify() so I think the formatting is fine.
Then, if possible, could you provide more code (on the client- and the server-side)? Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.