From d2a91b4d6da79ed8e73c63b18ea79fc660d84ea7 Mon Sep 17 00:00:00 2001 From: Maccesch Date: Thu, 31 Aug 2023 20:12:48 +0100 Subject: [PATCH] client opened bidir streams work --- examples/use_webtransport/src/log_display.rs | 12 ++++ examples/use_webtransport/src/main.rs | 59 +++++++++++++++---- examples/use_webtransport/src/stream_bidir.rs | 36 +++++++++++ examples/use_webtransport/src/stream_send.rs | 29 +++++++++ src/use_webtransport.rs | 24 ++++++-- 5 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 examples/use_webtransport/src/log_display.rs create mode 100644 examples/use_webtransport/src/stream_bidir.rs create mode 100644 examples/use_webtransport/src/stream_send.rs diff --git a/examples/use_webtransport/src/log_display.rs b/examples/use_webtransport/src/log_display.rs new file mode 100644 index 0000000..7d9e71f --- /dev/null +++ b/examples/use_webtransport/src/log_display.rs @@ -0,0 +1,12 @@ +use leptos::*; + +#[component] +pub fn LogDisplay(#[prop(into)] log: Signal>) -> impl IntoView { + view! { +
+
    + {move || log().iter().map(|l| view! {
  • {l}
  • }).collect::>()} +
+
+ } +} diff --git a/examples/use_webtransport/src/main.rs b/examples/use_webtransport/src/main.rs index 9113044..c85cfa2 100644 --- a/examples/use_webtransport/src/main.rs +++ b/examples/use_webtransport/src/main.rs @@ -3,10 +3,20 @@ use leptos_use::core::ConnectionReadyState; use leptos_use::docs::demo_or_body; use leptos_use::{use_webtransport_with_options, UseWebTransportOptions}; +mod log_display; +mod stream_bidir; +mod stream_send; + +use log_display::*; +use stream_bidir::*; +use stream_send::*; + #[component] fn Demo() -> impl IntoView { let (datagrams_log, set_datagrams_log) = create_signal(vec![]); + let id = store_value(0); + let transport = use_webtransport_with_options( "https://echo.webtransport.day", UseWebTransportOptions::default() @@ -15,16 +25,15 @@ fn Demo() -> impl IntoView { }) .on_close(move || { set_datagrams_log.update(|log| log.push("Connection closed".to_string())) - }) - .on_error(move |e| set_datagrams_log.update(|log| log.push(format!("Error: {:?}", e)))), + }), ); let (text, set_text) = create_signal("".to_string()); - let on_send = { + let on_send_datagrams = { let transport = transport.clone(); - move |e| { + move |_| { set_datagrams_log.update(|log| log.push(format!("Sent datagram: '{}'", text()))); transport.send_datagrams(text().as_bytes()); @@ -47,18 +56,48 @@ fn Demo() -> impl IntoView { false, ); + let (bidir_streams, set_bidir_streams) = create_signal(vec![]); + + let on_open_bidir_stream = { + let transport = transport.clone(); + + move |_| { + let transport = transport.clone(); + + spawn_local(async move { + match transport.create_bidir_stream().await { + Ok(bidir_stream) => { + let i = id.get_value(); + id.set_value(i + 1); + + set_bidir_streams.update(|s| s.push((i, bidir_stream))); + } + Err(e) => { + set_datagrams_log.update(|log| { + log.push(format!("Failed to open bidir stream: {:?}", e)) + }); + } + } + }); + } + }; + let ready_state = transport.ready_state; view! { +

Datagrams