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

标签 html node.js socket.io webrtc

我一直在尝试让 WebRTC 与广播公司和多个监听器一起运行,但在通过信令(使用 nodejs 和 socket.io)进行传输描述和候选者时遇到了困难。

我可以使用一个简单的nodejs套接字应用程序在两个浏览器之间运行该进程,该应用程序只需将描述和候选广播到其他已连接的客户端,但是当我尝试存储描述并与新打开的浏览器连接时,什么也没有发生。

我基本上需要了解的是,我需要向一个浏览器提供什么,以便它开始与另一个浏览器通信?我正在从事的项目要求听众能够加入房间、进行身份验证并开始收听正在发送的任何媒体。

下面是我的客户端代码:

    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”只能与另一个对等点一起使用。您无法缓存由一个 PeerConnection 实例生成的 Offer SDP 以便将其与多个其他对等点一起使用。

就您而言,您必须为要从中发送/接收音频和视频的每个浏览器创建一个 PeerConnection,然后通过信令机制与这些浏览器交换相应的 SDP 提供和答案。

请随意浏览一些链接I have mentioned在这里了解 WebRTC 的工作原理。

关于html - WebRTC:将多个监听器连接到一个客户端,一次一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21529845/

相关文章:

javascript - angularjs ng-change函数不会在html输入中正常触发

javascript - 为什么 Chrome 在包含 Javascript 文件后添加查询字符串参数?

android - 连接时出错 : io. socket.engineio.client.EngineIOException: xhr poll 错误

javascript - HTML5 Canvas 玩家限制

javascript - 通过已知半径 2D 的圆缠结点在 html Canvas 中绘制圆弧以适合 Angular

javascript - 如何不将插入符号 ^ 符号添加到用 yarn 安装的包中?

javascript - npm update v5.4.2 后 ./node_modules/react-router-dom 中找不到模块错误

node.js - JOI - 如何从一组字段中获取至少一个字段

node.js - Azure 移动服务和 socket.io

html - 没有空白的网格 div