首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拥有多个同级PeerJS?

拥有多个同级PeerJS?
EN

Stack Overflow用户
提问于 2015-03-24 03:22:47
回答 1查看 10.1K关注 0票数 3

下面是我用于连接PeerJS的代码:

代码语言:javascript
复制
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();
}

我借用了这段代码,我想知道在对等点之间有多个连接是可能的,比如一个呼叫组。我的假设是,当对等节点检测到有新的人在呼叫时,我需要做的就是添加一个新的音频对象,然后让不同的用户接收其他用户的it,并将音频对象添加到他们的页面。这样行得通吗?有没有更有效的方法呢?

EN

回答 1

Stack Overflow用户

发布于 2015-04-16 15:27:54

对等体可以维护传出的流的列表。例如,当一个对等体连接到系统时,所有其他对等体都将得到通知,并将该对等体的id保存到它们的列表中。当对等体尝试发起呼叫时,它将遍历其列表中的所有对等体。您可以查看的一个例子是Noah Burney的peerjs-audio-chat

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29218658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档