Skip to content

Commit

Permalink
add verbose 30s beginning, duration, and end
Browse files Browse the repository at this point in the history
Added more clarity about how long the Ping is intended to last, and made it a bit more obvious that the ConnectionClosed event is expected in the case of `Ping` after the idle timeout expires
  • Loading branch information
DougAnderson444 committed Dec 1, 2023
1 parent 1ea52f1 commit e27bd7a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions examples/browser-webrtc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ use web_sys::{Document, HtmlElement};
pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> {
tracing_wasm::set_as_global_default();

let ping_duration = Duration::from_secs(30);

let body = Body::from_current_window()?;
body.append_p("Let's ping the WebRTC Server!")?;
body.append_p(&format!(
"Let's ping the rust-libp2p server over WebRTC for {:?}:",
ping_duration
))?;

let mut swarm = libp2p::SwarmBuilder::with_new_identity()
.with_wasm_bindgen()
.with_other_transport(|key| {
webrtc_websys::Transport::new(webrtc_websys::Config::new(&key))
})?
.with_behaviour(|_| ping::Behaviour::new(ping::Config::new()))?
// Ping does not KeepAlive, so we set the idle connection timeout to 32_212_254u64,
// which is the largest value that works with the wasm32 target.
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(32_212_254u64)))
.with_swarm_config(|c| c.with_idle_connection_timeout(ping_duration))
.build();

let addr = libp2p_endpoint.parse::<Multiaddr>()?;
Expand All @@ -48,6 +51,19 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> {
tracing::info!("Ping successful: RTT: {rtt:?}, from {peer}");
body.append_p(&format!("RTT: {rtt:?} at {}", Date::new_0().to_string()))?;
}
SwarmEvent::ConnectionClosed {
cause: Some(cause), ..
} => {
tracing::info!("Swarm event: {:?}", cause);

if let libp2p::swarm::ConnectionError::KeepAliveTimeout = cause {
body.append_p("All done with pinging! ")?;

break;
} else {
body.append_p(&format!("Connection closed due to: {:?}", cause))?;
}
}
evt => tracing::info!("Swarm event: {:?}", evt),
}
}
Expand Down

0 comments on commit e27bd7a

Please sign in to comment.