3

PeerJS と接続するためのコードは次のとおりです。

var peer = new Peer({ key: '[PeerJSID]', debug: 3});
peer.on('open', function(){
  $('#my-id').text(peer.id);
});
// Receiving a call
peer.on('call', function(call){
  // Answer the call automatically (instead of prompting user) for demo purposes
  call.answer(window.localStream);
  step3(call);
});
peer.on('error', function(err){
  alert(err.message);
  // Return to step 2 if error occurs
  step2();
});
// Click handlers setup
$(function(){
  $('#make-call').click(function(){
    // Initiate a call!
    var call = peer.call($('#callto-id').val(), window.localStream);
    step3(call);
  });
  $('#end-call').click(function(){
    window.existingCall.close();
    step2();
  });
  // Retry if getUserMedia fails
  $('#step1-retry').click(function(){
    $('#step1-error').hide();
    step1();
  });
  // Get things started
  step1();
});
function step1 () {
  // Get audio stream
  navigator.getUserMedia({audio: true, video: false}, function(stream){
    // Set your video displays
    $('#my-video').prop('src', URL.createObjectURL(stream));
    window.localStream = stream;
    step2();
  }, function(){ $('#step1-error').show(); });
}
function step2 () {
  $('#step1, #step3').hide();
  $('#step2').show();
}
function step3 (call) {
  // Wait for stream on the call, then set peer video display
  call.on('stream', function(stream){
    $('#their-video').prop('src', URL.createObjectURL(stream));
  });
  // UI stuff
  window.existingCall = call;
  $('#their-id').text(call.peer);
  call.on('close', step2);
  $('#step1, #step2').hide();
  $('#step3').show();
}

このコードを拝借しましたが、コール グループのように、ピア間で複数の接続が可能ではないかと考えていました。私の仮説は、ピアが新しい人が電話をかけていることを検出したときに新しいオーディオ オブジェクトを追加し、それを別のユーザーが他のユーザーの ID を受け取り、ページにオーディオ オブジェクトを追加するだけでよいというものです。同じように。これは機能しますか?それを行うためのより効率的な方法はありますか?

4

1 に答える 1

0

ピアは、発信ストリームのリストを維持できます。たとえば、ピアがシステムに接続すると、他のすべてのピアに通知され、このピアの ID がリストに保存されます。ピアが通話を開始しようとすると、リスト内のすべてのピアをトラバースします。確認できる 1 つの例は、Noah Burney によるpeerjs-audio-chatです。

于 2015-04-16T07:27:53.760 に答える