Skip to content

Commit

Permalink
add a client example
Browse files Browse the repository at this point in the history
  • Loading branch information
TannerRogalsky committed Apr 12, 2024
1 parent 39dba57 commit 19f0b07
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use futures_util::{SinkExt, StreamExt};

#[tokio::main]
async fn main() {
let ws = tokio_tungstenite_wasm::connect("wss://echo.websocket.org/")
.await
.unwrap();
let (mut sender, mut receiver) = ws.split();

println!("Connected to echo server.");

let msg = receiver.next().await.unwrap().unwrap();
assert!(msg.into_text().unwrap().starts_with("Request served by"));

println!("Recieved initial connection message.");

let payload = "This is a test message.";
sender
.send(tokio_tungstenite_wasm::Message::text(payload))
.await
.unwrap();

println!("Sent payload to echo server. Waiting for response...");

let msg = receiver.next().await.unwrap().unwrap();
assert_eq!(msg, tokio_tungstenite_wasm::Message::text(payload));

println!("Received and validated response.")
}

0 comments on commit 19f0b07

Please sign in to comment.