2017-02-19 77 views
0

我有一個問題,即時通訊想要從FeederBot函數內訪問data.names,所以我可以訪問從客戶端命名的名稱。但我不知道如何。我想讓這個變量在feeder bot裏面訪問,這個.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

有很多的數據在你的代碼中,在每個套接字事件處理程序中。你想要哪一個? – 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 

只是你在哪裏定義瓦爾範圍一起來看看。 JS工程結構在這種情況下,你都不能訪問你的函數之前定義的var定義,並在您的函數相同範圍級別定義

編輯:爲更好地理解

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函數im想要this.nickname爲= data.names。 – exper

+0

你有沒有檢查過你在這兩個函數中的data.names都有相同的焦點?我認爲有分歧。導出到您之前定義的var,並且您可以從外部訪問它。 – mtizziani

+0

我真的不明白,因爲即時通訊不是很好的JavaScript,我的朋友給我發送了大部分的代碼添加到。 – exper