【问题标题】:RTCDataChannel.send Firefox problemsRTCDataChannel.send Firefox 问题
【发布时间】:2016-09-16 00:50:54
【问题描述】:

我正在使用 WebRTC 技术进行一些测试,但我在 Chrome 和 Firefox 之间的 RTCDataChannel 方面存在一些问题。连接工作正常,我可以在两个浏览器之间发送数据,但是当两个浏览器之一断开连接时就会出现问题。

当 Chrome 正在发送数据而 Firefox 接收数据并且 Firefox 断开连接时,Chrome 端的通道将其 readyState 设置为关闭,我可以处理此显示消息。

当 Firefox 正在发送数据而 Chrome 接收数据并且 Chrome 断开连接时,Firefox 端的通道 不会更改它的 readyState,所以我无法处理这个问题,显然 Firefox 继续向 ¿ 关闭的通道发送数据?

我的代码很简单:

建立连接:

[...]
if(offerer){
        channel = connection.createDataChannel("dataChannel", null);
        setChannelEvents(channel);
        connection.createOffer(myFunc, showError);
}else{
        connection.ondatachannel = function(event){
           channel = event.channel;
           setChannelEvents(channel);
        }
    }
[...]

当连接建立并可以发送消息时:

[...]
try{
  channel.send(message);
    }catch(e){
        //handle error here
        [...]
    }
[...]

正如我所说,在 Chrome 中时 channel.send(message);返回错误(因为 readyState 设置为关闭且无法发送消息)“此处处理错误”中的代码执行,但在 Firefox 中从不执行。

有人可以帮帮我吗?这有点令人沮丧。 谢谢!

【问题讨论】:

    标签: javascript google-chrome firefox webrtc


    【解决方案1】:

    试试我的代码:

    switch (channel.readyState) {
    case 'open':
        channel.send(message);
        break;
    case 'closed':
        console.log('channel.readyState = ' + channel.readyState);
        //handle error here
        break;
    default:
        console.error('channel.readyState = ' + channel.readyState);
    

    }

    我已经在 Chrome 和 Firefox 中测试成功

    【讨论】:

    • 我尝试了您的代码,但无法正常工作。我的问题是即使其他用户关闭频道,channel.readyState 也总是“打开”。所以,执行总是在“case 'open'”中进入:(
    • 目前我使用的是 Windows 10、Firefox 46.0.1 和 Chrome 51.0.2704.63。请告诉我您的操作系统版本和浏览器版本
    • 我正在使用 Mac OSX El Capitan 和 Chrome 50.0.2661.102(64 位)和 Firefox 46.0.1
    【解决方案2】:

    在接收端调用RTCDataChannel.close()方法。

    【讨论】:

    • 当前close方法是receiver调用的,问题是sender不知道channel已经关闭,因为他的readyState var一直处于'open' :(
    • 我没有任何想法 :( 如果您解决了您的问题,请通知我
    猜你喜欢
    • 2011-02-17
    • 2011-03-04
    • 2010-12-05
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多