标签:
在下是webrtc开发新手,目前遇到了个问题,翻来翻去一直没弄明白。可能英文不佳,看文档看蒙了,所以一直没找到解决方法。
开发环境:
node.js 搭建的服务器
目前是用socket.io做通信
开发目的:
A同学向B同学发起请求,B同学收到后双方互相视频直播。
如果有清楚的同学麻烦告知我哈,真的麻烦大家了 O(∩_∩)O~~
问题描述:
目前在搭建两人视频的demo,但是在双方信号交换信息上出问题了。控制台打印出来是这样的:
代码如下:
1 var servers = null; 2 3 function trace(text) { 4 console.log((performance.now() / 1000).toFixed(3) + ": " + text); 5 } 6 7 //创建PeerConnection实例 8 localPeerConnection = new RTCPeerConnection(servers); 9 trace("Created local peer connection object localPeerConnection"); 10 12 localPeerConnection.onicecandidate = function (evt){ 13 14 socket.send(JSON.stringify({ "candidate": evt.candidate })); 15 16 trace("Local ICE candidate: \n" + evt.candidate.candidate); 17 18 } 19 20 localPeerConnection.onaddstream = function (evt) { 21 remoteVideo.src = URL.createObjectURL(evt.stream); 22 trace("add remote stream to other peer") 23 }; 24 25 localPeerConnection.addStream(localStream); 26 trace("Added localStream to localPeerConnection"); 27 28 if (isCaller) { 29 localPeerConnection.createOffer(gotDescription, handleError); 30 31 } 32 else{ 33 localPeerConnection.createAnswer(localPeerConnection.remoteDescription, gotDescription); 34 } 35 36 } 37 38 function gotDescription(evt) { 39 40 localPeerConnection.setLocalDescription(evt); 41 socket.send(JSON.stringify({ "sdp": evt })); 42 } 43 44 socket.on(‘pushMessageDone‘, function (evt) { 45 46 if (!localPeerConnection){ 47 console.log(1); 48 call(false); 49 } 50 51 var signal = JSON.parse(evt); 52 53 if (signal.sdp){ 54 55 localPeerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)); 56 } else { 57 58 localPeerConnection.addIceCandidate(new RTCIceCandidate(signal.candidate)); 59 } 60 }); 61 62 63 function handleError(){}
标签:
原文地址:http://www.cnblogs.com/Travel/p/5461355.html