mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 21:39:21 +00:00
Send calls configuration to RTC relay
This commit is contained in:
parent
9d67565517
commit
a2b2fc89cf
@ -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<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct CallsConfigWrapper {
|
||||
title: String,
|
||||
data: CallsConfig,
|
||||
}
|
||||
|
||||
/// Current WebSocket connection
|
||||
static mut ACTIVE_RTC_CONNECTION: Option<Addr<RtcRelayActor>> = None;
|
||||
|
||||
impl actix::Actor for RtcRelayActor {
|
||||
type Context = actix_web_actors::ws::WebsocketContext<Self>;
|
||||
|
||||
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<Result<actix_web_actors::ws::Message, actix_web_actors::ws::ProtocolError>> for RtcRelayActor {
|
||||
@ -29,6 +82,23 @@ impl StreamHandler<Result<actix_web_actors::ws::Message, actix_web_actors::ws::P
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<RTCMessages> 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<Addr<RtcRelayActor>> {
|
||||
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)
|
||||
}
|
@ -39,6 +39,9 @@ pub enum Event<'a> {
|
||||
/// Deleted a comment
|
||||
DeletedComment(&'a Comment),
|
||||
|
||||
/// Connection to RTC relay was closed
|
||||
ClosedRTCRelayWebSocket,
|
||||
|
||||
/// No event
|
||||
None,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user