2013-06-28 27 views
2

我正在使用SimpleSignalling server來連接兩個對等端。的基本過程是:SyntaxError在WebRTC中發送遠程冰候選人

PubNub廣播對等方的創建的

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

對等體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) 
    } 
} 

onawakenonicecandidate處理程序似乎被正確執行。當兩個選項卡打開時,第二個將接收JSON候選對象。當我將它們傳遞給connections[uid].addIceCandidate時,出現錯誤TypeMismatchError:DOM Exception 17.

code和​​處於聯機狀態。


什麼傳遞給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。我在createAnswer中有setRemoteDescription,但回調永遠不會執行。


onoffer我說:

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

這清除了語法錯誤。 ICE消息現在正在成功發送,但connections[uid].ondatachannel永遠不會被調用。

回答

1

我爲createAnswer的結果添加了一個處理程序。該程序現在起作用。

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

工作代碼是一個連接任何用戶和打開頁面的牆上應用程序。它運行在wholcomb.github.io/SimpleSignaling/