问题描述
我在需要创建数据通道的时候使用 WebRTC JS 应用程序和 stcuk。我有这部分代码实际上可以工作,但是 openRTCDataChannel 方法被执行了两次:
this.myRTCConnections[id][hash]=window.RTCPeerConnection?new RTCPeerConnection(this.RTCConfiguration,{optional:[]}):(window.mozRTCPeerConnection?new mozRTCPeerConnection(this.RTCConfiguration,{optional:[]}):new webkitRTCPeerConnection(this.RTCConfiguration,{optional:[]}));
this.myRTCConnections[id][hash].ondatachannel=function(event){This.openRTCDataChannel(event,id,hash)}//first calling of openRTCDataChannel method
this.openRTCDataChannel(false,hash)//second calling of openRTCDataChannel method
this.openRTCDataChannel=function(event,hash,onOffer,initiator){
var RTCDataChannelOptions={reliable:true},This=this
if(!this.myRTCDataChannels[id])this.myRTCDataChannels[id]={}
this.myRTCDataChannels[id][hash]=event&&event.channel?event.channel:this.myRTCConnections[id][hash].createDataChannel("myDataChannel",RTCDataChannelOptions)
this.myRTCDataChannels[id][hash].onerror=function(error){log(error)}
this.myRTCDataChannels[id][hash].onmessage=function(e){This.handleIncomingRTcmessage(e.data,hash)}
this.myRTCDataChannels[id][hash].onclose=function(e){This.onSignalingServerLeave(false,hash)}
this.myRTCDataChannels[id][hash].onopen=function(e){This.onRTCDataChannelOpen(id,initiator)}
}
如果我第一次或第二次评论调用 if openRTCDataChannel 方法,我的一些同行可以在他们之间交换数据,而另一些则不能。
所以问题是,如果我想让我的代码工作,我需要以两种不同的方式执行 openRTCDataChannel 方法两次。我做错了什么,启动适用于所有浏览器的数据通道的最佳方法是什么?
感谢任何帮助!
解决方法
好的。所以看来我已经发现这里出了什么问题。
发起连接并发送报价的对等点应以这种方式创建数据通道:
this.openRTCDataChannel(false,id,hash)
另一个接受提议并发送答案的对等点应该使用这个:
this.myRTCConnections[id][hash].ondatachannel=function(event){This.openRTCDataChannel(event,hash)}
我在 Mozilla 和 Chrome 中检查了这个,它对我有用。如果我错了,请纠正我。