为了我的缘故,我正在逐步测试WebRTC程序.
我为无服务器的WebRTC编写了一些测试站点.
http://webrtcdevelop.appspot.com/
事实上,谷歌的STUN服务器被使用,但没有部署信令服务器.
会话描述协议(SDP)手动交换,即浏览器窗口之间的CopyPaste.
到目前为止,这是我用代码得到的结果:
'use strict'; var peerCon; var ch; $(document) .ready(function() { init(); $('#remotebtn2') .attr("disabled", ""); $('#localbtn') .click(function() { offerCreate(); $('#localbtn') .attr("disabled", ""); $('#remotebtn') .attr("disabled", ""); $('#remotebtn2') .removeAttr("disabled"); }); $('#remotebtn') .click(function() { answerCreate( new RTCSessionDescription(JSON.parse($('#remote') .val()))); $('#localbtn') .attr("disabled", ""); $('#remotebtn') .attr("disabled", ""); $('#remotebtn') .attr("disabled", ""); }); $('#remotebtn2') .click(function() { answerGet( new RTCSessionDescription(JSON.parse($('#remote') .val()))); $('#remotebtn2') .attr("disabled", ""); }); $('#msgbtn') .click(function() { msgSend($('#msg') .val()); }); }); var init = function() { //offer------ peerCon = new RTCPeerConnection( { "iceServers": [ { "url": "stun:stun.l.google.com:19302" }] }, { "optional": [] }); var localDescriptionOut = function() { console.log(JSON.stringify(peerCon.localDescription)); $('#local') .text(JSON.stringify(peerCon.localDescription)); }; peerCon.onicecandidate = function(e) { console.log(e); if (e.candidate === null) { console.log('candidate empty!'); localDescriptionOut(); } }; ch = peerCon.createDataChannel( 'ch1', { reliable: true }); ch.onopen = function() { dlog('ch.onopen'); }; ch.onmessage = function(e) { dlog(e.data); }; ch.onclose = function(e) { dlog('closed'); }; ch.onerror = function(e) { dlog('error'); }; }; var msgSend = function(msg) { ch.send(msg); } var offerCreate = function() { peerCon .createOffer(function(description) { peerCon .setLocalDescription(description, function() { //wait for complete of peerCon.onicecandidate }, error); }, error); }; var answerCreate = function(descreption) { peerCon .setRemoteDescription(descreption, function() { peerCon .createAnswer( function(description) { peerCon .setLocalDescription(description, function() { //wait for complete of peerCon.onicecandidate }, error); }, error); }, error); }; var answerGet = function(description) { peerCon.setRemoteDescription(description, function() { // console.log(JSON.stringify(description)); dlog('local-remote-setDescriptions complete!'); }, error); }; var error = function(e) { console.log(e); }; var dlog = function(msg) { var content = $('#onmsg') .html(); $('#onmsg') .html(content + msg + '
'); }
Firefox(26.0):
RtpDataChannels
onopen
事件成功触发,但send
失败.
Chrome(31.0):
RtpDataChannels
onopen
事件成功触发,send
也成功了.
Chrome的SDP对象如下:
{"sdp":".................. cname:L5dftYw3P3clhLve
\r\
na=ssrc:2410443476 msid:ch1 ch1
\r\
na=ssrc:2410443476 mslabel:ch1
\r\
na=ssrc:2410443476 label:ch1
\r\n","type":"offer"}
其中ch1信息在代码中定义;
ch = peerCon.createDataChannel( 'ch1', { reliable: false });
捆绑得当.
但是,Firefox的SDP对象(本地描述)根本不包含DataChannel,而且,SDP比Chrome短得多,捆绑的信息也少.
我错过了什么?
可能,我猜send
DataChannel失败的原因是由于firefox在SDP对象中缺少信息.
我怎么能解决这个问题?我调查了各种工作库的来源,比如peerJS,easyRTC,simpleWebRTC,但无法弄清楚原因.
任何建议和建议阅读表示赞赏.