Game Development Reference
In-Depth Information
send an object, a function, or anything else to the server, it will be available to the
server as a UTF-16 encoded string. If we send a JSON string to the server then all
we need to do is parse the data and access the specifics. However, if we simply send
an actual object, such as a literal JSON object, the server will receive something
such as the following code snippet:
// Client code
var con = new WebSocket
("ws://localhost:8888/packt/sockets/
multiplayer-game-server");
// …
con.send({name: "Rodrigo"});
// Server code
String input = get_input_from_socket();
input.toString() == "[object Object]";
Thus, when sending objects through a web socket, JavaScript will not attempt to en-
code the object, but will instead simply call the object's toString function and send
the output of that along to the socket.
Search WWH ::




Custom Search