From 13bf916652a777f399ec4fddf24d0278294e6b7f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 12 Feb 2021 16:46:11 +0100 Subject: [PATCH] Properly close connections --- src/controllers/calls_controller.rs | 32 ++++++++++++++++++++++--- src/controllers/rtc_relay_controller.rs | 8 ++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/controllers/calls_controller.rs b/src/controllers/calls_controller.rs index f0abb94..1471ed0 100644 --- a/src/controllers/calls_controller.rs +++ b/src/controllers/calls_controller.rs @@ -360,9 +360,25 @@ pub fn make_user_leave_call(conv_id: &ConvID, connection: &UserWsConnection) -> user_ws_controller::send_to_client(connection, &UserWsMessage::no_id_message("call_closed", conv_id)?)?; } - // TODO : call main stream (sender) + // Close main stream (sender) + events_helper::propagate_event(&Event::CloseCallStream(&CloseCallStream { + call_hash: gen_call_hash(&conv_id, &connection.user_id), + peer_id: None, + }))?; - // TODO : close receiver streams (other users streams) + // Close receiver streams (other users streams) + user_ws_controller::foreach_connection( + |peer_conn| { + if peer_conn.is_having_call_with_conversation(conv_id) && &peer_conn.user_id != &connection.user_id { + events_helper::propagate_event(&Event::CloseCallStream(&CloseCallStream { + call_hash: gen_call_hash(&conv_id, &peer_conn.user_id), + peer_id: Some(connection.user_id.clone()), + }))?; + } + + Ok(()) + }, + )?; // Create a notification events_helper::propagate_event(&Event::UserLeftCall(conv_id, &connection.user_id))?; @@ -419,7 +435,17 @@ pub fn handle_event(e: &events_helper::Event) -> Res { )?; } - // TODO : handle proxy disconnect => close all active calls + // Handle proxy disconnect => close all active calls + Event::ClosedRTCRelayWebSocket => { + user_ws_controller::foreach_connection(|f| { + // Close all active connections + if let Some(call) = &f.active_call { + make_user_leave_call(&call.conv_id, f)?; + } + + Ok(()) + })?; + } _ => {} } diff --git a/src/controllers/rtc_relay_controller.rs b/src/controllers/rtc_relay_controller.rs index 310007a..136bb35 100644 --- a/src/controllers/rtc_relay_controller.rs +++ b/src/controllers/rtc_relay_controller.rs @@ -188,7 +188,13 @@ impl Handler for RtcRelayActor { // Send message RTCMessages::SendMessage(msg) => { match serde_json::to_string(&msg) { - Ok(txt) => { ctx.text(txt) } + Ok(txt) => { + if conf().verbose_mode { + println!("API => RTC WS : {}", txt); + } + + ctx.text(txt) + } Err(e) => { eprintln!("Failed to send message to RTC relay ! {:#?}", e); }