在WebRTC中发送远程冰候选的SyntaxError

时间:2013-06-28 07:14:10

标签: javascript dom webrtc

我正在使用SimpleSignalling server来连接两个对等点。基本过程是:

PubNub广播对等A的创建

pubnub.subscribe( {
      channel : 'mimis/peer/awaken',
      message : function( msg ) {
          var dest_uid = msg
          if( server.uid() != dest_uid ) {
              onawaken( dest_uid )
          }
      },
  } )

Peer B创建一个新的PeerConnection

function onawaken( uid ) {
    var servers = null
    connections[uid] = new webkitRTCPeerConnection( servers,
                                                    { optional: [{ RtpDataChannels: true }] } )

    function gotCandidate( event ) {
        if( event.candidate ) {
            server.send( event.candidate, uid, server.room() )
        }
    }
    connections[uid].onicecandidate = gotCandidate

    try {
        channels[uid] = connections[uid].createDataChannel( "mimisChannel",
                                                            { reliable: false } )
    } catch (e) {
        console.error( 'Failed to create data channel. ' +
                       'You need Chrome M25 or later with RtpDataChannel enabled' )
    }

    function gotLocalDescription( desc ) {
        connections[uid].setLocalDescription( desc )
        server.send( desc, uid, server.room() )
    }

    connections[uid].createOffer( gotLocalDescription )
}

PeerConnection创建响应

function onoffer( offer, uid ) {
    connections[uid] = new webkitRTCPeerConnection( servers,
                                                    { optional: [{ RtpDataChannels: true }] } )

    function gotRemoteDescription( desc ) {
        connections[uid].setRemoteDescription( desc )
        server.send( desc, uid, server.room() )
    }

    connections[uid].createAnswer( gotRemoteDescription )

    function gotReceiveChannel( event ) {
        channels[uid] = event.channel
    }

    connections[uid].ondatachannel = gotReceiveChannel
}

ICE候选人从SimpleSignaling服务器收到消息。

server.onmessage = function( msg, uid, room ) {
    if( msg.type == 'offer' ) {
        onoffer( msg, uid )
    } else if( msg.candidate ) {
        try {
            connections[uid].addIceCandidate( msg )
        } catch( e ) {
            console.error( 'connections[uid].addIceCandidate', e )
        }
    } else {
        console.warn( 'server.onmessage: Unknown message type', msg )
    }
}

onawaken onicecandidate处理程序似乎正常运行。当打开两个选项卡时,第二个选项卡将接收JSON候选对象。当我将它们传递给connections[uid].addIceCandidate时,我得到错误`TypeMismatchError:DOM Exception 17。

codedemo在线。


传递给addIceCandidate的内容必须是RTCIceCandidate。最终代码如下:

server.onmessage = function( msg, uid, room ) {
      var candidate = new RTCIceCandidate( msg )
      connections[uid].addIceCandidate( candidate )
}

现在我得到SyntaxError: DOM Exception 12。从理论上讲,这是因为我calling addIceCandidate before setRemoteDescription。我在setRemoteDescription中有createAnswer,但回调永远不会被执行。


onoffer我添加了:

connections[uid].setRemoteDescription( new RTCSessionDescription( offer ) )

这清除了语法错误。 ICE消息现在已成功发送,但永远不会调用connections[uid].ondatachannel

1 个答案:

答案 0 :(得分:1)

我为createAnswer的结果添加了一个处理程序。该计划现在有效。

server.onmessage = function( msg, uid, room ) {
    if( msg.type == 'answer' ) {
        connections[uid].setRemoteDescription( new RTCSessionDescription( msg ) )
    }
}

工作代码是一个墙上应用程序,可以将任何用户与打开的页面连接起来。它运行在wholcomb.github.io/SimpleSignaling/