diff --git a/src/controllers/rtc_relay_controller.rs b/src/controllers/rtc_relay_controller.rs index 8a8c950..39e0ed8 100644 --- a/src/controllers/rtc_relay_controller.rs +++ b/src/controllers/rtc_relay_controller.rs @@ -2,15 +2,68 @@ //! //! @author Pierre Hubert -use actix::{ActorContext, StreamHandler}; +use actix::{ActorContext, Addr, AsyncContext, Handler, StreamHandler}; +use actix::prelude::*; use actix_web_actors::ws::{Message, ProtocolError}; +use serde::Serialize; use crate::data::config::conf; +use crate::helpers::events_helper; +use crate::helpers::events_helper::Event; struct RtcRelayActor {} +#[derive(Message)] +#[rtype(result = "()")] +enum RTCMessages { + CLOSE +} + +#[allow(non_snake_case)] +#[derive(Serialize)] +struct CallsConfig { + allowVideo: bool, + iceServers: Vec, +} + +#[derive(Serialize)] +struct CallsConfigWrapper { + title: String, + data: CallsConfig, +} + +/// Current WebSocket connection +static mut ACTIVE_RTC_CONNECTION: Option> = None; + impl actix::Actor for RtcRelayActor { type Context = actix_web_actors::ws::WebsocketContext; + + fn started(&mut self, ctx: &mut Self::Context) { + // Replace known address of actor + unsafe { + ACTIVE_RTC_CONNECTION = Some(ctx.address()); + } + + println!("Started new WebSocket connection to RTC relay!"); + + // Send calls configuration to server + ctx.text(serde_json::to_string(&CallsConfigWrapper { + title: "config".to_string(), + data: CallsConfig { + allowVideo: conf().rtc_relay.as_ref().unwrap().allow_video, + iceServers: conf().rtc_relay.as_ref().unwrap().ice_servers.clone(), + }, + }).unwrap()) + } + + fn stopped(&mut self, _ctx: &mut Self::Context) { + println!("Closed connection to RTC relay."); + + // Propagate information + if let Err(e) = events_helper::propagate_event(&Event::ClosedRTCRelayWebSocket) { + eprintln!("Failed to propagate rtc closed event! {:#?}", e); + } + } } impl StreamHandler> for RtcRelayActor { @@ -29,6 +82,23 @@ impl StreamHandler for RtcRelayActor { + type Result = (); + + fn handle(&mut self, _msg: RTCMessages, ctx: &mut Self::Context) -> Self::Result { + ctx.close(None) + } +} + +/// Get current actix connection +fn get_active_connection() -> Option> { + let conn; + unsafe { + conn = ACTIVE_RTC_CONNECTION.clone(); + } + conn +} + /// Establish a new connection with the RTC relay /// /// Debug with @@ -63,6 +133,11 @@ pub async fn open_ws(req: actix_web::HttpRequest, return Ok(actix_web::HttpResponse::Unauthorized().body("Invalid token!")); } + // Close previous connection (if any) + if let Some(conn) = get_active_connection() { + conn.do_send(RTCMessages::CLOSE); + } + // Start the actor actix_web_actors::ws::start(RtcRelayActor {}, &req, stream) } \ No newline at end of file diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index 5ccb4cc..e668706 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -39,6 +39,9 @@ pub enum Event<'a> { /// Deleted a comment DeletedComment(&'a Comment), + /// Connection to RTC relay was closed + ClosedRTCRelayWebSocket, + /// No event None, }