2 个版本
0.1.1 | 2024 年 3 月 30 日 |
---|---|
0.1.0 | 2024 年 3 月 29 日 |
#1807 在 网络编程
每月 39 次下载
65KB
1K SLoC
Just WebRTC
使用 Rust 编写的简单、快速、易于使用的 WebRTC 对等节点。
支持在 native
和 wasm32
目标上使用 WebRTC,并为两者提供相同的 API。
just-webrtc
是模块化的,只包含 WebRTC 类型实现。这里没有信令!
自己搭建信令设置,或者使用在 just-webrtc-signalling
上的现成标准信令客户端/服务器
... WebRTC 可能会变得简单?
[dependencies]
just-webrtc = "0.1"
文档
请参阅 docs.rs 以获取完整的 API 参考。
示例
此基本示例创建了一个具有单个数据通道的 '本地' 和 '远程' 对等节点。
请参阅 data_channels
测试,以获取此示例的编译版本。
有关完整示例,包括信令,请参阅仓库中的 示例。
对等节点 A - 本地对等节点(从配置中创建本地对等节点)
注意:"本地对等节点" 是 WebRTC 术语,指的是创建 offer 的初始对等节点。在内部,此对等节点也称为 "offerer"。
use anyhow::Result;
use just_webrtc::{
DataChannelExt,
PeerConnectionExt,
SimpleLocalPeerConnection,
types::{SessionDescription, ICECandidate}
};
async fn run_local_peer() -> Result<()> {
// create simple local peer connection with unordered data channel
let mut local_peer_connection = SimpleLocalPeerConnection::build(false).await?;
// output offer and candidates for remote peer
let offer = local_peer_connection.get_local_description().await.unwrap();
let candidates = local_peer_connection.collect_ice_candidates().await?;
// ... send the offer and the candidates to Peer B via external signalling implementation ...
let signalling = (offer, candidates);
// ... receive the answer and candidates from Peer B via external signalling implementation ...
let (answer, candidates) = signalling;
// update local peer from received answer and candidates
local_peer_connection.set_remote_description(answer).await?;
local_peer_connection.add_ice_candidates(candidates).await?;
// local signalling is complete! we can now wait for a complete connection
local_peer_connection.wait_peer_connected().await;
// receive data channel from local peer
let mut local_channel = local_peer_connection.receive_channel().await.unwrap();
// wait for data channels to be ready
local_channel.wait_ready().await;
// send data to remote (answerer)
local_channel.send(&bytes::Bytes::from("hello remote!")).await?;
// recv data from remote (answerer)
let recv = local_channel.receive().await.unwrap();
assert_eq!(&recv, "hello local!");
Ok(())
}
对等节点 B - 远程对等节点(从接收到的 offer 中创建远程对等节点)
注意:"远程对等节点" 是 WebRTC 术语,指的是从 "本地对等节点" 接收 offer 的对等节点。在内部,此对等节点也称为 "answerer"。
use anyhow::Result;
use just_webrtc::{
DataChannelExt,
PeerConnectionExt,
SimpleRemotePeerConnection,
types::{SessionDescription, ICECandidate}
};
async fn run_remote_peer(offer: SessionDescription, candidates: Vec<ICECandidate>) -> Result<()> {
// ... receive the offer and the candidates from Peer A via external signalling implementation ...
// create simple remote peer connection from received offer and candidates
let mut remote_peer_connection = SimpleRemotePeerConnection::build(offer).await?;
remote_peer_connection.add_ice_candidates(candidates).await?;
// output answer and candidates for local peer
let answer = remote_peer_connection.get_local_description().await.unwrap();
let candidates = remote_peer_connection.collect_ice_candidates().await?;
// ... send the answer and the candidates back to Peer A via external signalling implementation ...
let _signalling = (answer, candidates);
// remote signalling is complete! we can now wait for a complete connection
remote_peer_connection.wait_peer_connected().await;
// receive data channel from local and remote peers
let mut remote_channel = remote_peer_connection.receive_channel().await.unwrap();
// wait for data channels to be ready
remote_channel.wait_ready().await;
// send/recv data from local (offerer) to remote (answerer)
let recv = remote_channel.receive().await.unwrap();
assert_eq!(&recv, "hello remote!");
// send/recv data from remote (answerer) to local (offerer)
remote_channel.send(&bytes::Bytes::from("hello local!")).await?;
Ok(())
}
许可证
此项目受以下任一许可证的许可:
- Apache 许可证第 2 版
- MIT 许可证供您选择。
依赖关系
~0.6–17MB
~248K SLoC