2016-07-29 2 views
0

peer.js를 사용 중이고 들어오는 모든 연결을 배열에 저장하려고합니다.피어 연결을 배열에 저장하는 방법

응용 프로그램 배경 : 컴퓨터가 주 콘솔 역할을합니다. 전화기는 콘솔 피어 ID를 사용하여 콘솔에 연결하고 콘솔에 피어 ID를 보냅니다. 그런 다음 콘솔은 각 피어 ID를 읽고 데이터 연결을 만듭니다.

배열에 연결을 저장하려고하는데 필요한 모든 기능으로 연결을 호출 할 수 있습니다. 연결을 'SendPhonePlayerDetails'함수에 전달하려고하면 연결이 정의되지 않은 것으로 인쇄됩니다.

//establish a connection 
//data.pid is the peer id of the phone 
connections[current_player] = peer.connect(data.pid); 

setTimeout(function() { 
    sendPhonePlayerDetails(connections[current_player]); 
},'2000'); 

function sendPhonePlayerDetails(connection) 
{ 
    console.log(connection) //prints undefined; 
    player_num = Object.size(players); //get the player number 
    player_name = players["p" + player_num][1] //get the players name 

    //send data to the phone 
    setTimeout(function() { 
     send_data({player_number: player_num, player_name: player_name }, connection); 
    },'2000'); 

    //if not player 1 notify them who is the leader 
    if(player_num > 1){ 
     setTimeout(function() { 
      send_data({controlling: players["p1"][1] }, connection); 
     },'2000'); 
    } 

} 


function send_data(data, connection) 
{ 
    if (!connection) return; 
    connection.send(data); //send the data to the phone 
} 
+0

'current_player'와 같은 소리는 유효한 참조가 아닙니다. 그 가치를 확인하고 있습니까? – mscdex

+0

예. 현재 콘솔에 로그를 남길 수 있습니다. # @mscdex –

+0

'console.dir (Object.keys (connections))'라면, (최초의) 타임 아웃 콜백 시점에서'current_player'에 포함 된 키를 보여줄 것입니까? – mscdex

답변

0

연결 배열이 전역으로 해석되지 않았습니다. '창.'을 추가하여 로그를 콘솔하고 연결 배열을 전달할 수있었습니다.

setTimeout(function() { 
    sendPhonePlayerDetails(window.connections['p' + (current_player-1)]); 
},'2000'); 
관련 문제