Javascript WebRTC-rtpeerConnection.localDescription在Firefox中返回null,但在Chrome中正常工作
我使用WebRTC构建了一个简单的流媒体服务。我目前仍在通过localhost运行所有程序。当使用Chrome浏览器时,目前一切正常,但当我使用Firefox时,我无法连接。我正在使用WebRTC适配器垫片 问题似乎源于peerConnection.localDescription始终等于null,并且无法将我的localDescription发送给对等方,或者无法正确设置remoteDescription 下面是我的代码片段。这只覆盖流的接收者,即发起p2p连接的接收者。拖缆已经设置了本地流,并设置了自己的本地和远程描述,然后将本地描述发送给收件人。sendRecipientDescription()只处理通过套接字将sdp发送到拖缆。PC_配置仅包括一个STUN服务器:Javascript WebRTC-rtpeerConnection.localDescription在Firefox中返回null,但在Chrome中正常工作,javascript,google-chrome,firefox,webrtc,Javascript,Google Chrome,Firefox,Webrtc,我使用WebRTC构建了一个简单的流媒体服务。我目前仍在通过localhost运行所有程序。当使用Chrome浏览器时,目前一切正常,但当我使用Firefox时,我无法连接。我正在使用WebRTC适配器垫片 问题似乎源于peerConnection.localDescription始终等于null,并且无法将我的localDescription发送给对等方,或者无法正确设置remoteDescription 下面是我的代码片段。这只覆盖流的接收者,即发起p2p连接的接收者。拖缆已经设置了本地流,
setUpRecipient = () => {
this.createPeerConnection();
this.pc
.createOffer({ offerToReceiveVideo: true })
.then(offer => {
this.pc.setLocalDescription(offer);
})
.then(() => {
this.sendRecipientDescription();
console.log('recipient local description ', this.pc.localDescription);
})
.catch(e => {
console.log('error recipient set up ', e);
});
};
createPeerConnection = () => {
try {
this.pc = new RTCPeerConnection(PC_CONFIG);
this.pc.onicecandidate = this.handleIceCandidate;
this.pc.ontrack = this.handleRemoteStreamAdded;
this.pc.onremovetrack = this.handleRemoteStreamRemoved;
this.pc.oniceconnectionstatechange = this.handleIceStateChange;
console.log('Created RTCPeerConnection', this.pc.localDescription);
} catch (e) {
console.log('Failed to create PeerConnection, exception: ', e.message);
}
};
使用Chrome浏览器时,this.pc.localDescription会按预期返回。使用Firefox浏览器时,this.pc.localDescription始终为空,根本没有RTCSessionDescription。当我在setLocalDescription之后输入console.log(this.pc)时,似乎localDescription确实为空:
但是,当我展开RTCPeerConnection对象时,您会看到localDescription似乎设置正确:。但是,当我尝试发送this.pc.localDescription时,它只发送null。我找到了我自己问题的答案。显然我需要返回这个.pc.setLocalDescription()
我不知道为什么这是必要的。据我所知,pc.setLocalDescription不返回任何内容,只具有设置pc.localDescription的副作用。它在Chrome中工作得非常好,但在Firefox中却不行。
setLocalDescription
返回了一个承诺。如果没有return语句,承诺链将不会等待异步操作完成,然后继续sendRecipientDescription
。你有一个比赛条件,恰巧在Chrome中工作,而不是在FF中,因为时间在某一点上是不同的。感谢noppa的解释,我很感激。