This question concerns the WebSocket API(i.e. var webSocketServer = require('websocket').server;) in NodeJS.
function Game(canvas) {
this.wArray;
this.runConnection();
// I want to be able to see changes in variables at this point
console.log(this.wArray[1][2]); // is out of scope or something
}
_p = Game.prototype;
_p.runConnection = function() {
this.connection = new WebSocket('ws://localhost:1337');
this.connection.onmessage = function (message) {
this.wArray = JSON.parse(message.data);
};
// code here runs before code inside onmessage, it must be asychronous
};
So I when I receive a message from the server, I should be able to take that message and update some variables and such in my code. At the moment, it seems all I can do is update things that are inside of the onmessage function. All of the examples online simply show people using console.log() inside of onmessage. I want the server to be able to send my client information, and then use that information to update certain aspects of my game as it is running. I think that there is some level of asychronousness about onmessage().
Please show me how to take the data that is passed to me via WebSocket.onmessage() and store it in variables that can be accessed throughout my game.