use embedded_io_adapters::tokio_1::FromTokio; use std::error::Error; use tokio::net::TcpStream; use embedded_websocket_embedded_io::{ framer_async::{Framer, FramerError, ReadResult}, WebSocketClient, WebSocketCloseStatusCode, WebSocketOptions, WebSocketSendMessageType, }; #[tokio::main] async fn main() -> Result<(), FramerError> { // Connect to a peer let address = "127.0.0.1:1337"; let mut buffer = [0u8; 4000]; let tcp_stream = TcpStream::connect(address).await.map_err(FramerError::Io)?; let mut stream = FromTokio::new(tcp_stream); let websocket = WebSocketClient::new_client(rand::thread_rng()); // initiate a websocket opening handshake let websocket_options = WebSocketOptions { path: "/chat", host: "localhost", origin: "http://localhost:1337", sub_protocols: None, additional_headers: None, }; let mut framer = Framer::new(websocket); framer .connect(&mut stream, &mut buffer, &websocket_options) .await?; println!("ws handshake complete"); framer .write( &mut stream, &mut buffer, WebSocketSendMessageType::Text, true, "Hello, world".as_bytes(), ) .await?; println!("sent message"); while let Some(read_result) = framer.read(&mut stream, &mut buffer).await { let read_result = read_result?; match read_result { ReadResult::Text(text) => { println!("received text: {text}"); framer .close( &mut stream, &mut buffer, WebSocketCloseStatusCode::NormalClosure, None, ) .await? } _ => { // ignore other kinds of messages } } } println!("closed"); Ok(()) }