#yrs #web-rtc #crdt #协议 #更新 #信令 #连接器

yrs-webrtc

Yjs/Yrs文档更新交换的WebRTC网络连接器

2个不稳定版本

0.2.0 2023年11月25日
0.1.0 2023年5月5日

#1229 in 网页开发

MIT 许可证

46KB
806

yrs-webrtc

这是一个Yrs集成网络提供者,它利用WebRTC协议来传播更改。它与y-webrtc JavaScript客户端兼容,并使用相同的信令服务器协议(提供了Node.jsRust对该协议的实现供您使用)。

示例

use yrs_webrtc::signal::PeerEvent;
use yrs_webrtc::{Error, Room, SignalingConn};

// start a WebRTC peer 
async fn peer(topic: &str) -> Result<(), Error> {
    // create a signaling client
    let conn = Arc::new(SignalingConn::connect("ws://127.0.0.1:8000/signaling").await?);
    // create a new peer collaborating on a given topic
    let room = Room::open(topic, Awareness::default(), [conn]);
    let mut peer_events = room.peer_events().subscribe();

    // wait for room to initialize connection to signaling server
    room.connect().await?;

    // listen for events about connecting/disconnecting peers
    let _ = tokio::spawn(async move {
        while let Ok(e) = peer_events.recv().await {
            println!("received peer event: {e:?}");
        }
    });

    // watch for incoming updates
    let _ = {
        let awareness = room.awareness().write().await;
        awareness.doc().observe_update_v1(move |txn, u| {
            let u = Update::decode_v1(&u.update).unwrap();
            println!("received update: {u:?}");
        })
    };

    // keep room alive and close it when you're done
    room.close().await?;
}

// create y-webrtc/yrs-webrtc compatible signaling server
use yrs_warp::signaling::{signaling_conn, SignalingService};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let signaling = SignalingService::new();

    let ws = warp::path("signaling")
        .and(warp::ws())
        .and(warp::any().map(move || signaling.clone()))
        .and_then(ws_handler);

    warp::serve(ws).run(([0, 0, 0, 0], 8000)).await;
    Ok(())
}

async fn ws_handler(ws: Ws, svc: SignalingService) -> Result<impl Reply, Rejection> {
    Ok(ws.on_upgrade(move |socket| ws_conn(socket, svc)))
}

async fn ws_conn(ws: WebSocket, svc: SignalingService) {
    match signaling_conn(ws, svc).await {
        Ok(_) => println!("signaling connection stopped"),
        Err(e) => eprintln!("signaling connection failed: {}", e),
    }
}

依赖项

~33–49MB
~1M SLoC