add close

This commit is contained in:
mii443
2024-09-01 00:32:04 +09:00
parent be6cd228cb
commit 078d310e09
2 changed files with 8 additions and 4 deletions

View File

@ -62,7 +62,8 @@ async fn chat(mut p2p: P2P) -> Result<()> {
println!("P2P Closed!"); println!("P2P Closed!");
} }
}); });
let peer_connection = p2p.peer_connection.clone();
let send_thread = tokio::spawn(async move { let send_thread = tokio::spawn(async move {
loop { loop {
let line = tokio::task::spawn_blocking(|| read_line()).await.unwrap(); let line = tokio::task::spawn_blocking(|| read_line()).await.unwrap();
@ -72,8 +73,11 @@ async fn chat(mut p2p: P2P) -> Result<()> {
tokio::select! { tokio::select! {
_ = receive_thread => {}, _ = receive_thread => {},
_ = send_thread => {} _ = send_thread => {},
_ = tokio::signal::ctrl_c() => {}
} }
Ok(()) peer_connection.lock().await.close().await.unwrap();
std::process::exit(0);
} }

View File

@ -21,7 +21,7 @@ pub struct ConnectionCode {
} }
pub struct P2P { pub struct P2P {
peer_connection: Arc<Mutex<RTCPeerConnection>>, pub peer_connection: Arc<Mutex<RTCPeerConnection>>,
send_data_tx: Arc<Mutex<Sender<SendData>>>, send_data_tx: Arc<Mutex<Sender<SendData>>>,
send_data_rx: Arc<Mutex<Receiver<SendData>>>, send_data_rx: Arc<Mutex<Receiver<SendData>>>,
pub receive_data_rx: Arc<Mutex<Receiver<Receive>>>, pub receive_data_rx: Arc<Mutex<Receiver<Receive>>>,