이 질문은 NodeSS의 WebSocket API (예 : var webSocketServer = require ('websocket'). server;)와 관련이 있습니다.WebSocket.onmessage() 외부 변수 액세스하기
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
};
그래서 서버에서 메시지를 받으면 메시지를 받고 코드에서 일부 변수 등을 업데이트 할 수 있어야합니다. 현재 내가 할 수있는 일은 onmessage 함수 안에있는 것들을 업데이트하는 것입니다. 모든 온라인 예제는 onmessage 내에 console.log()를 사용하는 사람들을 보여줍니다. 서버가 클라이언트 정보를 보내고 그 정보를 사용하여 실행중인 게임의 특정 부분을 업데이트 할 수 있기를 원합니다. 나는 onmessage()에 대해 어떤 수준의 비동기 성이 있다고 생각합니다.
WebSocket.onmessage()를 통해 나에게 전달 된 데이터를 가져와 내 게임에서 액세스 할 수있는 변수에 저장하는 방법을 보여주십시오.