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
}
4

1 に答える 1