From 19f0b07a12c73d3c58151bc03277614ff31b1e9d Mon Sep 17 00:00:00 2001 From: Tanner Rogalsky Date: Fri, 12 Apr 2024 08:06:13 -0400 Subject: [PATCH] add a client example --- examples/client.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/client.rs diff --git a/examples/client.rs b/examples/client.rs new file mode 100644 index 0000000..a252287 --- /dev/null +++ b/examples/client.rs @@ -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.") +}