Hi, I am trying to write a server accepting HTTP requests and WebSocket connections. The HTTP request should come from the browser to get the HTML of a web app, and the web app will make a request for opening a web socket connection. I am looking to use tokio-tungstenite to handle web sockets, because my server is built on tokio, so it is highly asynchronous:
async fn handle_websocket(stream: tokio::net::TcpStream) -> Result<(), Error> {
let websocket = tokio_tungstenite::accept_async(stream).await?;
println!("WebSocket accepted");
websocket.for_each(async move |result| {
println!("{:#?}", result);
}).await;
Ok(())
}
I have been using async_h1 before to handle HTTP requests:
async fn handle_http_request(stream: async_std::net::TcpStream) -> Result<(), Error> {
async_h1::accept(stream, |req| async move {
Self::handle_request(req).await
})
.await
.map_err(Into::into)
}
But this library is not compatible with the tokio::net::TcpStream
s, because they use different libraries to provide AsyncRead
and AsyncWrite
traits, specifically, tokio uses its own traits (tokio::io::AsyncRead
) and async_h1 uses futures_io::AsyncRead
.
So I would like to drop async_h1
, because I want to use the same TcpStream
type, so that I can use a single TcpListener
(tokio::net::TcpListener
).
But I can't find a crate compatible with tokio that provides a similar functionality as the async_h1::server::accept
function, i.e. taking a TcpStream
, reading HTTP requests and processing them asynchronously (returning a future).
Does anyone have an idea what I could do or use?
Thanks a lot