2017-02-19 2 views
0

私は問題があります。私はFeederBot関数内からdata.namesにアクセスしたいので、クライアントから発信された名前にアクセスできます。しかし、私は方法を知らない。変数をthis.nickname = data.namesのフィーダーボット内でアクセスしたい。どんな助け?関数外の変数にアクセスする必要があります

io.on('connection', function(socket) { 

    socket.on('login', function(data) { 
     console.log("User connected with id:" + data.uuid + " Bot names: " + data.names); 
     socket.room = data.uuid; 
     socket.join(data.uuid); 

     if (data.type == "server") { 
      io.sockets.in(socket.room).emit("force-login", "server-booted-up"); 
     } 
    }.bind(this)); 

    socket.on('pos', function(data) { 
     //console.log(socket.room + " : " + data); 
     io.sockets.in(socket.room).emit('pos', data); 
    }); 

    socket.on('cmd', function(data) { 
     console.log(data); 
     io.sockets.in(socket.room).emit('cmd', data); 
    }); 

    socket.on("spawn-count", function(data) { 
     io.sockets.in(socket.room).emit("spawn-count", data); 
    }); 

    socket.emit("force-login", "startup"); 

}); 

function FeederBot(bot_id, agent, bot_number, server) { 

    this.bot_id = bot_id; //ID of bot for logging 
    this.interval_id = 0; //here we will store setInterval's ID 
    this.ball_id = null; 
    this.server = ''; //server address will be stored here 
    this.client = new AgarioClient('Bot_' + this.bot_id); //creates new client 
    this.client.debug = 0; 
    this.client.agent = agent; 
    this.client.headers['user-agent'] = config.userAgent; 
    if (facebookManager.hasAvailableToken()) { 
     this.client.auth_token = facebookManager.getToken(); 
    } 
    this.isOnFeedMission = false; this.nickname = "Exper" 

    this.lastsent = {minx: 0, miny: 0, maxx: 0, maxy: 0}; 
    this.onboard_client(server, bot_number); 
} 
+0

のがたくさんありますあなたのコードの 'data'を各ソケットイベントハンドラに置き換えます。あなたはどれが欲しいですか? – RomanPerekhrest

+0

ログインハンドラから – exper

答えて

1
var outsideVar = null; 

function functionOne() { 
    outsideVar = 1; 
} 

function functionTwo() { 
    console.log(outsideVar); 
} 

functionOne(); // set the value of your var 
functionTwo(); // output in console should be 1 

ちょうどあなたがあなたのVARSを定義スコープの表情を持っています。この場合には構造化し、あなたの関数が定義される前に定義されているVARSにアクセスし、あなたの関数のように同じスコープレベルにすることができますJSの作品は

編集に定義されています。より良い理解のための

var dataStorage = null; // before you enter any { scope with socket 

{ 
    // ... 
    socket.on('login', function(data) { 

    dataStorage = data; // <<- write data to the global var dataStorage 

    console.log("User connected with id:" + data.uuid + " Bot names: " +  data.names); 
    socket.room = data.uuid; 
    socket.join(data.uuid); 

    if (data.type == "server") {  
     io.sockets.in(socket.room).emit("force-login", "server-booted-up"); 
    } 

    }.bind(this)); 
    // .. 
} 

function FeederBot(bot_id, agent, bot_number, server) { 

    this.bot_id = bot_id; //ID of bot for logging 
    this.interval_id = 0; //here we will store setInterval's ID 
    this.ball_id = null; 
    this.server = ''; //server address will be stored here 
    this.client = new AgarioClient('Bot_' + this.bot_id); //creates new client 
    this.client.debug = 0; 
    this.client.agent = agent; 
    this.client.headers['user-agent'] = config.userAgent; 
    if (facebookManager.hasAvailableToken()) { 
    this.client.auth_token = facebookManager.getToken(); 
    } 
    this.isOnFeedMission = false; 
    this.nickname = "Exper" 

    // and here you can access the global var again. 
    // make sure u test for not null and the property exists 
    if(!!dataStorage) { 
    if(dataStorage.hasOwnProperty('names')){ 
     this.nickname = data.names; 
    } 
    } 

    this.lastsent = {minx: 0, miny: 0, maxx: 0, maxy: 0}; 
    this.onboard_client(server, bot_number); 

} 
+0

ログインハンドラの内部にはdata.namesがあります。それは、使用しているクライアントから放射されています。 feederbot関数の中でthis.nicknameを= data.namesにしたい。 – exper

+0

両方の機能のdata.namesに同じ焦点を当てていることを確認しましたか?私は違いがあると思う。前にdefindeしたvarにエクスポートし、外部からアクセスすることができます。 – mtizziani

+0

私は本当に理解していないように、私はjavascriptでうまくいきませんでした。私の友人は、私に、ほとんどのコードを送ってくれました。 – exper

関連する問題