diff --git a/src/main.rs b/src/main.rs index 8ec8d82..c87ba7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use futures_util::StreamExt; use log::info; use tokio::net::{TcpListener, TcpStream}; use tokio::runtime::Builder; +use tokio_tungstenite::WebSocketStream; //use tungstenite::protocol::Message; fn main() -> Result<(), Error> { @@ -30,29 +31,22 @@ fn main() -> Result<(), Error> { let listener = TcpListener::bind(&addr).await.expect("Failed to bind"); info!("Listening on: {}", addr); while let Ok((stream, _)) = listener.accept().await { - tokio::spawn(accept_connection(stream)); + tokio::spawn(nostr_server(stream)); } }); Ok(()) } -async fn accept_connection(stream: TcpStream) { +async fn nostr_server(stream: TcpStream) { let addr = stream .peer_addr() .expect("connected streams should have a peer address"); info!("Peer address: {}", addr); - - let ws_stream_res = tokio_tungstenite::accept_async(stream).await; - - match ws_stream_res { + let conn = tokio_tungstenite::accept_async(stream).await; + match conn { Ok(ws_stream) => { info!("New WebSocket connection: {}", addr); - let (_write, mut read) = ws_stream.split(); - // TODO: error on binary messages - // TODO: error on text messages > MAX_EVENT_SIZE - while let Some(mes_res) = read.next().await { - println!("got {:?}", mes_res); - } + process_client(ws_stream).await; } Err(_) => { println!("Error"); @@ -60,3 +54,11 @@ async fn accept_connection(stream: TcpStream) { } }; } +async fn process_client(stream: WebSocketStream) { + let (_write, mut read) = stream.split(); + // TODO: error on binary messages + // TODO: error on text messages > MAX_EVENT_SIZE + while let Some(mes_res) = read.next().await { + println!("got {:?}", mes_res); + } +}