mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-14 23:19:07 -05:00
Separate server and connection handling functions
This commit is contained in:
parent
ee27d0c597
commit
dbb535eaaa
26
src/main.rs
26
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<TcpStream>) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user