WebRTC:将多个监听器连接到一个客户端,一次一个

我一直在试图让WebRTC与广播器和多个监听器一起工作,但是当通过信号传输(使用nodejs&socket.io)传输描述和候选者时,我却陷入了困境。

我可以通过一个简单的nodejs套接字应用程序在两个浏览器之间工作,它只是将描述和候选人广播给其他已经连接的客户端,但是当我尝试存储描述并与新打开的浏览器连接时,什么都不会发生。

我基本上需要了解的是,我需要为一个浏览器提供什么,才能开始与另一个浏览器进行通信? 我正在进行的项目需要听众join会议室进行身份validation,并开始聆听正在发送的任何媒体。

以下是我的客户端代码:

var audioContext = new webkitAudioContext() var client = null var configuration = { 'iceServers': [{ 'url': 'stun:stun.example.org' }] } $(function () { window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection client = new RTCPeerConnection(configuration, { optional:[ { RtpDataChannels: true } ]}) client.onnegotiationneeded = function () { console.log('Negotiation needed') createOffer() } client.onicecandidate = function (event) { console.log('onicecandidate') socket.emit('candidate', JSON.stringify({ 'candidate': event.candidate })) } client.onaddstream = function (event) { console.log('onaddstream') $('#player').attr('src', URL.createObjectURL(event.stream)) player.play() } socket.on('candidate', function (event) { candidate(event) }) socket.on('description', function (message) { if(!client) { return } client.setRemoteDescription(new RTCSessionDescription(message.sdp), function () { if (client.remoteDescription.type == 'offer') client.createAnswer(function (description) { client.setLocalDescription(description, function () { socket.emit('description', JSON.stringify({ 'sdp':client.localDescription })) }) }, function (err) { console.log('error: ' + err) }) }, function(err) { console.log('error: ' + err) }) }) addStream() }) function createOffer () { if(!client) { return; } client.createOffer(function (description) { console.log(description) client.setLocalDescription(description, function () { socket.emit('description', JSON.stringify({ 'sdp': client.localDescription })) console.log('set local description') }) }) } function candidate (message) { if(message.candidate) { console.log('candidate') client.addIceCandidate(new RTCIceCandidate(message.candidate)) } } function addStream () { navigator.webkitGetUserMedia({audio: true, video: false}, function(stream) { client.addStream(stream) }) } 

我的服务器的信号部分,因为它目前的立场:

 io.on 'connection', (socket) -> socket.on 'description', (data) -> parsed = JSON.parse data socket.broadcast.emit 'description', parsed socket.on 'candidate', (candidate) -> parsed = JSON.parse candidate socket.broadcast.emit 'candidate', parsed 

我将不胜感激。 谢谢。

作为名称的“PeerConnection”表示只能与另一个对等体一起使用。 您不能caching由一个PeerConnection实例生成的报价SDP,以便将其用于多个其他对等方。

在你的情况下,你必须为每个你想要发送/接收audio和video的浏览器创build一个PeerConnection,然后通过你的信号机制与这些浏览器交换相应的SDP offer和答案。

请随意浏览我在这里提到的一些链接,以了解WebRTC的工作原理。