Skip to content

Commit

Permalink
Merge pull request #9 from TannerRogalsky/client-example
Browse files Browse the repository at this point in the history
Add a client example.
  • Loading branch information
TannerRogalsky committed Apr 30, 2024
2 parents 39dba57 + 19f0b07 commit 3e2045d
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 3e2045d

Please sign in to comment.