2024-06-13 19:16:55 +00:00
|
|
|
use futures::{SinkExt, StreamExt};
|
|
|
|
use tokio::{self, io, net::TcpSocket};
|
|
|
|
|
2024-12-18 16:59:06 +00:00
|
|
|
use tokio_xmpp::{
|
|
|
|
minidom::Element,
|
|
|
|
parsers::stream_features::StreamFeatures,
|
|
|
|
xmlstream::{accept_stream, StreamHeader, Timeouts},
|
|
|
|
};
|
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?;
|
2024-08-10 13:05:42 +00:00
|
|
|
let stream = accept_stream(
|
|
|
|
tokio::io::BufStream::new(stream),
|
|
|
|
tokio_xmpp::parsers::ns::DEFAULT_NS,
|
2024-08-18 15:40:39 +00:00
|
|
|
Timeouts::default(),
|
2024-08-10 13:05:42 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let stream = stream.send_header(StreamHeader::default()).await?;
|
|
|
|
let mut stream = stream
|
2024-12-18 16:59:06 +00:00
|
|
|
.send_features::<Element>(&StreamFeatures::default())
|
2024-08-10 13:05:42 +00:00
|
|
|
.await?;
|
2024-06-13 19:16:55 +00:00
|
|
|
|
|
|
|
tokio::spawn(async move {
|
2024-08-10 13:05:42 +00:00
|
|
|
while let Some(packet) = stream.next().await {
|
2024-06-13 19:16:55 +00:00
|
|
|
match packet {
|
|
|
|
Ok(packet) => {
|
|
|
|
println!("Received packet: {:?}", packet);
|
2024-08-10 13:05:42 +00:00
|
|
|
stream.send(&packet).await.unwrap();
|
2024-06-13 19:16:55 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Error: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|