xmpp-rs-mirror/tokio-xmpp/examples/echo_server.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2024-06-13 19:16:55 +00:00
use futures::{SinkExt, StreamExt};
use tokio::{self, io, net::TcpSocket};
use tokio_util::codec::Framed;
2024-06-15 13:21:20 +00:00
use tokio_xmpp::XmppCodec;
2024-06-13 19:16:55 +00:00
#[tokio::main]
async fn main() -> Result<(), io::Error> {
// TCP socket
let address = "127.0.0.1:5222".parse().unwrap();
let socket = TcpSocket::new_v4()?;
socket.bind(address)?;
let listener = socket.listen(1024)?;
// Main loop, accepts incoming connections
loop {
let (stream, _addr) = listener.accept().await?;
// Use the `XMPPCodec` to encode and decode frames
2024-06-15 13:21:20 +00:00
let mut framed = Framed::new(stream, XmppCodec::new());
2024-06-13 19:16:55 +00:00
tokio::spawn(async move {
while let Some(packet) = framed.next().await {
match packet {
Ok(packet) => {
println!("Received packet: {:?}", packet);
framed.send(packet).await.unwrap();
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
}
});
}
}