Skip to content

Commit 0f28528

Browse files
fix(webrtc example): clarify idle connection timeout
When I ran the `example/browser-webrtc` example I discovered it would break after a ping or two. The `Ping` idle timeout needed to be extended, on both the server and the wasm client, which is what this PR fixes. I also added a small note to the README about ensuring `wasm-pack` is install for the users who are new to the ecosystem. Fixes: #4950. Pull-Request: #4966.
1 parent f12dabc commit 0f28528

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

examples/browser-webrtc/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ It uses [wasm-pack](https://rustwasm.github.io/docs/wasm-pack/) to build the pro
55

66
## Running the example
77

8+
Ensure you have `wasm-pack` [installed](https://rustwasm.github.io/wasm-pack/).
9+
810
1. Build the client library:
911
```shell
1012
wasm-pack build --target web --out-dir static

examples/browser-webrtc/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@ use web_sys::{Document, HtmlElement};
1515
pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> {
1616
tracing_wasm::set_as_global_default();
1717

18+
let ping_duration = Duration::from_secs(30);
19+
1820
let body = Body::from_current_window()?;
19-
body.append_p("Let's ping the WebRTC Server!")?;
21+
body.append_p(&format!(
22+
"Let's ping the rust-libp2p server over WebRTC for {:?}:",
23+
ping_duration
24+
))?;
2025

2126
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
2227
.with_wasm_bindgen()
2328
.with_other_transport(|key| {
2429
webrtc_websys::Transport::new(webrtc_websys::Config::new(&key))
2530
})?
2631
.with_behaviour(|_| ping::Behaviour::new(ping::Config::new()))?
27-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5)))
32+
.with_swarm_config(|c| c.with_idle_connection_timeout(ping_duration))
2833
.build();
2934

3035
let addr = libp2p_endpoint.parse::<Multiaddr>()?;
@@ -46,6 +51,18 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> {
4651
tracing::info!("Ping successful: RTT: {rtt:?}, from {peer}");
4752
body.append_p(&format!("RTT: {rtt:?} at {}", Date::new_0().to_string()))?;
4853
}
54+
SwarmEvent::ConnectionClosed {
55+
cause: Some(cause), ..
56+
} => {
57+
tracing::info!("Swarm event: {:?}", cause);
58+
59+
if let libp2p::swarm::ConnectionError::KeepAliveTimeout = cause {
60+
body.append_p("All done with pinging! ")?;
61+
62+
break;
63+
}
64+
body.append_p(&format!("Connection closed due to: {:?}", cause))?;
65+
}
4966
evt => tracing::info!("Swarm event: {:?}", evt),
5067
}
5168
}

examples/browser-webrtc/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async fn main() -> anyhow::Result<()> {
3838
.with_behaviour(|_| ping::Behaviour::default())?
3939
.with_swarm_config(|cfg| {
4040
cfg.with_idle_connection_timeout(
41-
Duration::from_secs(30), // Allows us to observe the pings.
41+
Duration::from_secs(u64::MAX), // Allows us to observe the pings.
4242
)
4343
})
4444
.build();

0 commit comments

Comments
 (0)