本文介绍了RTCDataChannel发送方法不发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RTCDataChannel上遇到了一个奇怪的问题.

I'm having a strange problem with RTCDataChannel.

我正在对WebRTC进行一些研究,并且已经在进行WebRTC音频/视频聊天.现在,我想使用RTCDataChannel添加文本聊天和文件共享.

I'm doing some research on WebRTC and I have working WebRTC audio/video chat already. Now I wanted to add text chat and file sharing to it using RTCDataChannel.

我已经这样创建了RTCDataChannel:

I've created RTCDataChannel like this:

var dataChannelOptions = {
    reliable: true,
    maxRetransmitTime: "2000"
};

dataChannel = yourConnection.createDataChannel("testDataChannel", dataChannelOptions);

dataChannel.onerror = function (error) {
    console.log("dataChannel.OnError:", error);
};

dataChannel.onmessage = function (event) {
    console.log("dataChannel.OnMessage:", event);
};

dataChannel.onopen = function (event) {
    console.log("dataChannel.OnOpen", event);
    dataChannel.send("Hello World!");
};

dataChannel.onclose = function (event) {
    console.log("dataChannel.OnClose", event);
};

而且我唯一能接收到的是从 dataChannel.onopen 的第一行开始的日志.我没有收到来自 dataChannel.onmessage 的日志.

And only thing that I receieve on both sides is log from first line of dataChannel.onopen. I don't receive log from dataChannel.onmessage.

没有错误.

当我手动调用 dataChannel.send 时,结果是相同的.

When I manually call dataChannel.send result is the same.

经过以下测试:
谷歌浏览器(50.0.2661.94)
Firefox(45.0.2)

Tested on:
Google Chrome (50.0.2661.94)
Firefox (45.0.2)

任何人都可以帮忙吗?

推荐答案

这是人们常犯的错误,您在两个浏览器上都创建了数据通道,但在两个浏览器上均不接受,则需要使用RTCPeerConnection的 ondatachannel 事件并设置侦听器

this is a common mistake people make, you are creating datachannel on both browsers, but not accepting on either, you need use the RTCPeerConnection's ondatachannel event and set the listeners

这篇关于RTCDataChannel发送方法不发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 02:18