4 个版本
0.2.0 | 2024年4月5日 |
---|---|
0.1.2 | 2024年3月30日 |
0.1.1 | 2024年3月29日 |
0.1.0 | 2024年3月29日 |
16 在 #peer-connection
174 每月下载次数
63KB
1K SLoC
Just WebRTC Signalling
为 Rust 中的全网格 WebRTC 连接提供简单、快速和简单的信号处理。
提供可配置 TLS 的服务器和兼容 native
和 wasm32
的客户端。
此信号处理实现围绕一个 tonic
gRPC 服务构建。gRPC 被用于其效率、安全性、可伸缩性和负载均衡。在未来,互操作性也可能用于在不同语言编写的客户端之间进行通信。
just-webrtc-signalling
是模块化的。使用 just-webrtc
或任何你喜欢的 WebRTC 实现!
[dependencies]
just-webrtc-signalling = "0.1"
文档
查看 docs.rs 以获取完整的 API 参考。
特性标志
所有特性 [ client
、server
和 server_web ] 默认启用。
client
:启用信号客户端(兼容native
和wasm
)server
:启用原生信号服务(用于与原生客户端通信)server-web
:启用 Web 信号服务(用于与 Web/wasm 客户端通信)
示例
运行客户端
以下示例演示了如何使用您选择的 WebRTC 实现集成 just-webrtc-signalling
客户端。
注意:对于 Web 客户端,您需要禁用 server
和 server-web
特性,因为这些仅适用于原生客户端。
[dependencies]
just-webrtc-signalling = { version = "0.1", default-features = false, features = ["client"] }
对于完整示例,使用 just-webrtc
作为 WebRTC 实现,请参阅 GitHub 仓库中的示例。
use anyhow::Result;
use futures_util::FutureExt;
use just_webrtc_signalling::client::RtcSignallingClientBuilder;
/// run all client signalling concurrently
async fn run_peer(server_address: String) -> Result<()> {
// create signalling client
let signalling_client = RtcSignallingClientBuilder::default().build(server_address)?;
// start a peer
let mut signalling_peer = signalling_client.start_peer().await?;
// set callbacks for the new peer
signalling_peer.set_on_create_offer(create_offer);
signalling_peer.set_on_receive_answer(receive_answer);
signalling_peer.set_on_local_sig_cplt(local_sig_cplt);
signalling_peer.set_on_receive_offer(receive_offer);
signalling_peer.set_on_remote_sig_cplt(remote_sig_cplt);
// run the signalling peer
loop {
signalling_peer.step().await?;
}
Ok(())
}
// For the purpose of this example, the descriptions and candidates will be strings.
// In actual code, these types would be serializable types provided by the WebRTC implementation
type Description = String;
type ICECandidates = String;
/// implement create offer callback (called by the signalling client to create an offer)
async fn create_offer(remote_id: u64) -> Result<(u64, (Description, ICECandidates))> {
// User's WebRTC implementation creates a local peer connection here.
// Generating an offer and ICE candidates and returning them.
// The signalling client will deliver the signal to the remote peer with the corresponding `remote_id`
let offer = Description::new();
let offerer_candidates = ICECandidates::new();
Ok((remote_id, (offer, offerer_candidates)))
}
/// implement receive answer callback (called by the signalling client to deliver an answer)
async fn receive_answer(remote_id: u64, answer_set: (Description, ICECandidates)) -> Result<u64> {
// User's WebRTC implementation receives an answer from a remote peer to a local peer connection here.
Ok(remote_id)
}
/// implement 'local' (offerer peer) signalling complete callback
async fn local_sig_cplt(remote_id: u64) -> Result<u64> {
// User's WebRTC implementation may now open/receive data channels
// and perform send/receives across channels.
Ok(remote_id)
}
/// implement receive offer callback (called by the signalling client to deliver an offer and create an answer)
async fn receive_offer(remote_id: u64, offer_set: (Description, ICECandidates)) -> Result<(u64, (Description, ICECandidates))> {
// User's WebRTC implementation creates a remote peer connection from the remote offer and candidates here.
// Generating an answer and ICE candidates and returning them.
// The signalling client will deliver the signal to the remote peer with the corresponding `remote_id`
let answer = Description::new();
let answerer_candidates = ICECandidates::new();
Ok((remote_id, (answer, answerer_candidates)))
}
/// implement 'remote' (answerer peer) signalling complete callback
async fn remote_sig_cplt(remote_id: u64) -> Result<u64> {
// User's WebRTC implementation may now open/receive data channels
// and perform send/receives across channels.
Ok(remote_id)
}
运行服务器
运行 just-webrtc-signalling
服务器非常简单。只需配置并创建您需要的服务,然后并发运行即可!
您还可以将 RtcSignallingService
与现有 tonic
服务器一起使用,以提供额外的信号处理功能。
以下示例运行了两个服务,一个用于本地客户端,另一个用于Web客户端。这些服务共享通用的信号通道,以实现Web和本地之间的跨平台信号。
use std::sync::Arc;
use anyhow::Result;
use just_webrtc_signalling::{server::Signalling, DEFAULT_NATIVE_SERVER_ADDR, DEFAULT_WEB_SERVER_ADDR};
async fn serve() -> Result<()> {
// create shared mapped signalling channels
let signalling = Arc::new(Signalling::new());
// create service futures
let serve_fut = just_webrtc_signalling::server::serve(
signalling.clone(),
DEFAULT_NATIVE_SERVER_ADDR.parse()?,
None, None, // no keepalive interval or timeout
None, // no TLS
);
let serve_web_fut = just_webrtc_signalling::server::serve_web(
signalling,
DEFAULT_WEB_SERVER_ADDR.parse()?,
None, None, // no keepalive interval or timeout
None, // no TLS
);
// run native and web facing services concurrently
tokio::try_join!(serve_fut, serve_web_fut)?;
Ok(())
}
许可证
本项目采用以下任一许可证授权:
- Apache许可证,版本2.0
- MIT许可证(根据您的选择)。
依赖项
~1–15MB
~176K SLoC