2016-04-04 15 views
3
ardından

gelen aramaya cevap vermek benim kodudur:RTCPeerConnection.createAnswer geri arama WebRTC'de için mozilla tanımsız nesne sohbet döndürür

var pc = connection.pc; 
pc.setRemoteDescription(sdp,function() { 
pc.createAnswer(function(answer) { 
    pc.setLocalDescription(answer,function() { 
    // code for sending the answer 
}) 
}) 
}) 

Yukarıdaki kod krom için çalışıyor, ama mozilla aynı çalıştırdığınızda, pc.createAnswer numaralı telefondan alınan answer, geri bildirim undefined'dur. bana aşağıdaki hata veren bir sonucu olarak:

TypeError: Argument 1 of RTCPeerConnection.setLocalDescription is not an object.

cevap

1

sorundur Özellikle, hataları kontrol etmediğinizde: Gerekli hata geri çağrıları geçmediğine.

setRemoteDescription ve setRemoteDescription ya three arguments (eski geri arama stili) ya da one (sözlerini) gerektirir, ama ikisi de geçiyoruz. createAnswer için bir eksi aynı.

Tarayıcının JS bağlamaları hatalı yüklemeyi sonlandırır, size kontrol etmediğiniz bir sözü döndürür, etkin bir şekilde yutkunma hataları.

var pc = connection.pc; 
pc.setRemoteDescription(sdp, function() { 
    pc.createAnswer(function(answer) { 
    pc.setLocalDescription(answer, function() { 
     // code for sending the answer 
    }, function(e) { 
     console.error(e); 
    }); 
    }, function(e) { 
    console.error(e); 
    }); 
}, function(e) { 
    console.error(e); 
}); 

Ya Modern söz API kullanmak:

Ya gerekli hata geri aramalar eklemek

var pc = connection.pc; 
pc.setRemoteDescription(sdp) 
    .then(() => pc.createAnswer()) 
    .then(answer => pc.setLocalDescription(answer)) 
    .then(() => { 
    // code for sending the answer 
    }) 
    .catch(e => console.error(e)); 

söz API Firefox'ta doğal olarak mevcuttur, ya da Chrome'da adapter.js içinden. Bakınız fiddle.

Ve her zaman hataları denetleyin. ;)

+0

MDN'yi güncelledim. – jib