use rustwave::{Client, Result};
use serde::Serialize;
use std::io::Write;
use tokio::io::{AsyncBufReadExt, BufReader};
#[derive(Serialize)]
struct ChatMessage {
room: String,
text: String,
}
#[tokio::main]
async fn main() -> Result<()> {
let client = Client::connect("ws://127.0.0.1:8080").await?;
client.on("chat", |data| async move {
println!("Room message:> {}", data);
});
let room = "chat";
client.emit("join-room", room).await?;
let mut stdin = BufReader::new(tokio::io::stdin());
loop {
print!("you> ");
std::io::stdout().flush().unwrap();
let mut line = String::new();
if stdin.read_line(&mut line).await.unwrap() == 0 {
break;
}
let text = line.trim().to_string();
if text.is_empty() {
continue;
}
let msg = ChatMessage {
room: room.to_string(),
text,
};
client.emit("chat", msg).await.unwrap();
}
Ok(())
}