【问题标题】:WebRTC - What is the proper way to create RTC Data Channel?WebRTC - 创建 RTC 数据通道的正确方法是什么?
【发布时间】:2021-07-16 10:39:08
【问题描述】:

我在需要创建数据通道的时候使用 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,id,hash)//second calling of openRTCDataChannel method
    
        this.openRTCDataChannel=function(event,id,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,id,hash)}
            this.myRTCDataChannels[id][hash].onclose=function(e){This.onSignalingServerLeave(false,id,hash)}
            this.myRTCDataChannels[id][hash].onopen=function(e){This.onRTCDataChannelOpen(id,hash,onOffer,initiator)}
        }

如果我在第一次或第二次调用 openRTCDataChannel 方法时发表评论,我的一些同行可以在他们之间交换数据,而另一些则不能。

所以问题是,如果我想让我的代码工作,我需要以两种不同的方式执行两次 openRTCDataChannel 方法。我做错了什么,启动适用于所有浏览器的数据通道的最佳方式是什么?

任何帮助表示赞赏!

【问题讨论】:

    标签: javascript webrtc rtcpeerconnection rtcdatachannel


    【解决方案1】:

    好的。所以看来我发现这里出了什么问题。

    发起连接并发送报价的对等方应以这种方式创建数据通道:

    this.openRTCDataChannel(false,id,hash)
    

    另一个接受提议并发送答案的同行应该使用这个:

    this.myRTCConnections[id][hash].ondatachannel=function(event){This.openRTCDataChannel(event,id,hash)}
    

    我在 Mozilla 和 Chrome 中检查了这个,它对我有用。如果我错了,请纠正我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多