use fast_websocket_client::WebSocket;
use tokio::time::{Duration, sleep};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), fast_websocket_client::WebSocketClientError> {
let ws = WebSocket::new("wss://echo.websocket.org").await?;
ws.on_open(|_| async move {
println!("[OPEN] WebSocket connection opened.");
})
.await;
ws.on_close(|_| async move {
println!("[CLOSE] WebSocket connection closed.");
})
.await;
ws.on_message(|message| async move {
println!("[MESSAGE] {}", message);
})
.await;
sleep(Duration::from_secs(2)).await;
for i in 1..5 {
let message = format!("#{}", i);
if let Err(e) = ws.send(&message).await {
eprintln!("[ERROR] Send error: {:?}", e);
break;
}
println!("[SEND] {}", message);
sleep(Duration::from_secs(2)).await;
}
ws.close().await;
ws.await_shutdown().await;
Ok(())
}