webrtc学习笔记二(datachannel)

https://www.webrtc-experiment.com/DataChannel/
https://www.npmjs.com/package/datachannel.io
datachannel.io
官方:
http://www.html5rocks.com/en/tutorials/webrtc/datachannels/?redirect_from_locale=zh


本地调用的传数据的datachannel

<!DOCTYPE html>
<html>
<head><title>RTCDataChannel</title></head>
<body>
<button id="startButton" onclick="createConnection()">Start</button>
<button id="sendButton" onclick="sendData()">Send</button>
<button id="closeButton" onclick="closeDataChannels()">Stop</button>
<textarea id="dataChannelSend" >abc</textarea>
<textarea id="dataChannelReceive" ></textarea>
<script type="text/javascript">
var localPeerConnection, remotePeerConnection;//用于peer跟peer之间呼叫和建立连接以便传输音视频数据流
var sendChannel, receiveChannel;//用于peer跟peer之间传输音视频之外的一般数据。

function trace(text) {
console.log((window.performance.now() / 1000).toFixed(3) + ': ' + text);
}

function createConnection() {
var servers = null;
localPeerConnection = window.localPeerConnection =new webkitRTCPeerConnection(servers,{optional: [{RtpDataChannels: true}]});
remotePeerConnection = window.remotePeerConnection = new webkitRTCPeerConnection(servers,{optional: [{RtpDataChannels: true}]});
try {
sendChannel = localPeerConnection.createDataChannel('sendDataChannel',{reliable: false});
} catch (e) {
alert('createDataChannel() failed with exception: ' + e.message);
}
localPeerConnection.onicecandidate = function(event) {
if (event.candidate) {
remotePeerConnection.addIceCandidate(event.candidate);
trace('★★★★★Local ICE candidate: \n' + event.candidate.candidate);
}
}
remotePeerConnection.onicecandidate = function(event) {
if (event.candidate) {
localPeerConnection.addIceCandidate(event.candidate);
trace('★★★Remote ICE candidate: \n ' + event.candidate.candidate);
}
};

sendChannel.onopen = trace('--Send channel open state is : ' +sendChannel.readyState);;
sendChannel.onclose = trace('--Send channel close state is: ' +sendChannel.readyState);;

remotePeerConnection.ondatachannel = function(event) {
receiveChannel = event.channel;
receiveChannel.onmessage = function(event) {
document.getElementById('dataChannelReceive').value = event.data;
};
receiveChannel.onopen = trace('--Receive channel open state is: ' + receiveChannel.readyState);;
receiveChannel.onclose = trace('--Receive channel close state is: ' + receiveChannel.readyState);;
};

localPeerConnection.createOffer(gotLocalDescription);//
}

function sendData() {
var data = document.getElementById('dataChannelSend').value;
sendChannel.send(data);
}

function closeDataChannels() {
console.log("------close");
sendChannel.close();
receiveChannel.close();
localPeerConnection.close();
remotePeerConnection.close();
localPeerConnection = null;
remotePeerConnection = null;
}

function gotLocalDescription(desc) {
trace("createOffer gotLocalDescription--->");
localPeerConnection.setLocalDescription(desc);
// trace('------|||||----Offer from localPeerConnection \n' + desc.sdp);
remotePeerConnection.setRemoteDescription(desc);
remotePeerConnection.createAnswer(gotRemoteDescription);//
}

function gotRemoteDescription(desc) {
trace("createAnswer gotRemoteDescription--->");
remotePeerConnection.setLocalDescription(desc);
//trace('------------->>>>>>>>>>>>Answer from remotePeerConnection \n' + desc.sdp);
localPeerConnection.setRemoteDescription(desc);
}
</script>

</body>
</html>

### WebRTC连接与DataChannel连接的区别及使用场景 WebRTC是一种支持浏览器之间实时音视频通信的技术,其核心功能包括音视频采集、编解码、网络传输等。WebRTC连接通常包含多个子系统,如RTCPeerConnection用于音视频传输,而DataChannel则是WebRTC中专门用于传输任意用户数据的通道。两者在功能、传输方式及适用场景上存在明显差异。 #### 功能与传输方式 WebRTC连接主要用于音视频流的传输,其核心组件RTCPeerConnection负责建立端到端的加密连接,并通过SRTP协议传输音视频数据。这种连接方式通常涉及ICE(Interactive Connectivity Establishment)协议进行NAT穿透,确保不同网络环境下的设备能够成功建立连接。WebRTC连接还支持STUN和TURN服务器,以应对更复杂的网络拓扑[^1]。 DataChannel则专注于在浏览器之间传输任意数据,不涉及音视频编解码或同步问题。它基于SCTP(Stream Control Transmission Protocol)协议,提供可靠和有序的数据传输,同时也支持部分有序和不可靠传输模式,以适应不同的应用需求。DataChannel的优势在于其点对点的通信方式,使得数据可以直接在浏览器之间传输,而无需通过服务器中转,从而降低了延迟和服务器负载。 #### 使用场景 WebRTC连接主要应用于实时音视频通信场景,如视频会议、在线教育、远程医疗等。这些场景通常需要高质量的音视频传输,并依赖WebRTC的自适应码率控制、网络质量探测等功能来确保用户体验。WebRTC连接还支持多人会议场景,通过SFU(Selective Forwarding Unit)或MCU(Multipoint Control Unit)架构实现多路音视频流的转发与合成。 DataChannel适用于需要低延迟、点对点数据交换的场景,如在线协作工具(协同文档编辑、实时绘图)、游戏同步、文件传输等。例如,在在线协作场景中,用户对文档的修改可以通过DataChannel实时同步到其他参与者,而不需要依赖服务器中转,从而提升响应速度。在游戏场景中,DataChannel可以用于玩家状态同步,确保多人游戏中的实时交互体验。 #### 性能与扩展性 WebRTC连接由于涉及音视频编码、传输、解码等多个环节,整体资源消耗较大。它通常需要较高的带宽支持,尤其是在高清视频传输或多人会议场景下。WebRTC连接也更容易受到网络抖动和丢包的影响,因此需要复杂的拥塞控制机制来维持稳定传输。 DataChannel则相对轻量,其数据传输不涉及媒体编解码,因此CPU和带宽占用较低。DataChannel可以与WebRTC的音视频通道共存,共享底层的ICE连接,从而避免重复建立连接的开销。此外,DataChannel支持自定义数据格式,使得其在传输效率和灵活性方面具有优势[^1]。 #### 示例代码:使用WebRTC DataChannel传输文本消息 ```javascript // 创建RTCPeerConnection实例 const pc = new RTCPeerConnection(); // 创建DataChannel const dc = pc.createDataChannel("myChannel"); // 监听打开事件 dc.onopen = () => { console.log("DataChannel opened"); dc.send("Hello from sender"); }; // 监听接收事件 dc.onmessage = (event) => { console.log("Received message: " + event.data); }; // 建立ICE连接 pc.onicecandidate = (event) => { if (event.candidate) { // 将candidate信息发送给远端 } }; ``` 上述代码展示了如何在WebRTC中创建DataChannel并实现基本的消息传输。通过这种方式,可以在浏览器之间直接交换任意数据,而无需依赖服务器中转。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值