【问题标题】:creating WebRTC data channels after peerConnection establishedpeerConnection 建立后创建 WebRTC 数据通道
【发布时间】:2013-03-10 16:21:21
【问题描述】:

有没有办法在对等连接已经建立时创建数据通道?

这是我正在做的事情:

peerConnection.onstatechange = function(event){
        var state = peerConnection.readyState;
        console.log('state changed to : '+state);
        if(state==='stable'){
            console.log('connection is stable');
            var dataChannel = peerConnection.createDataChannel('test',{reliable: false});
            dataChannel.onopen = function(){
                console.log('data channel opened');
                dataChannel.send('hello data channel');
            };
            peerConnection.ondatachannel = function(event){
                console.log('ondatachannel event fire ',event);
            };              
        }
    };

它给了我以下输出:

state changed to : have-local-offer
state changed to : stable 
connection is stable 
  1. 如何确保真正建立了连接?我发现here stable state 等于 active sat。

  2. onopen 事件由于某种原因没有触发,所以我仍然不确定是否已建立连接。

如果您需要更多代码,请告诉我。

编辑: 我添加了onnegotiationneeded 事件处理程序,现在peerConnection.ondatachannel 正在触发,但通道处于connecting 状态

【问题讨论】:

  • 您找到解决方案了吗?
  • 有点,但还是很不稳定

标签: webrtc


【解决方案1】:

你可能遇到了和我一样的问题。显然,您必须在创建报价之前添加一个数据通道,如果您想添加一个流,您将需要创建一个新的报价/答案并重新协商。根据 W3C 规范:

http://www.w3.org/TR/webrtc/

特别是,如果 RTCPeerConnection 对象正在使用 MediaStream,并且通过调用 add() 方法将轨道添加到流的 MediaStreamTrackList 对象之一,则 RTCPeerConnection 对象必须触发“negotiationneeded”事件。移除媒体组件也必须触发“negotiationneeded”。

但是,FF 中还没有构建协商需要的事件(我认为在 Chrome 中),所以这就是为什么事情可能会表现得有点不稳定。

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    相关资源
    最近更新 更多