Extract constants and add config loader + example appsettings for niom-webrtc; use STUN constant in media manager

This commit is contained in:
ghost 2025-09-29 19:20:33 +02:00
parent 676a7fae24
commit 2250859ff9
5 changed files with 45 additions and 9 deletions

5
appsettings.example.json Normal file
View File

@ -0,0 +1,5 @@
{
"server": {
"stun_server": "stun:stun.l.google.com:19302"
}
}

24
src/config.rs Normal file
View File

@ -0,0 +1,24 @@
use serde::Deserialize;
use std::path::Path;
#[derive(Debug, Deserialize, Clone)]
pub struct ServerOptions {
pub stun_server: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub server: ServerOptions,
}
impl Config {
pub fn from_file<P: AsRef<Path>>(p: P) -> Result<Self, Box<dyn std::error::Error>> {
let s = std::fs::read_to_string(p)?;
let cfg: Config = serde_json::from_str(&s)?;
Ok(cfg)
}
pub fn load_default() -> Result<Self, Box<dyn std::error::Error>> {
Self::from_file("appsettings.json")
}
}

4
src/constants.rs Normal file
View File

@ -0,0 +1,4 @@
// Central constants for niom-webrtc
pub const DEFAULT_STUN_SERVER: &str = "stun:stun.l.google.com:19302";
pub const ASSET_FAVICON: &str = "/assets/favicon.ico";
pub const ASSET_MAIN_CSS: &str = "/assets/main.css";

View File

@ -3,6 +3,8 @@
mod components;
mod models;
mod utils;
mod constants;
mod config;
use dioxus::prelude::*;
use log::Level;
@ -30,10 +32,10 @@ fn App() -> Element {
#[component]
pub fn Content() -> Element {
let mut peer_id = use_signal(|| "peer-loading...".to_string());
let mut remote_id = use_signal(|| String::new());
let mut connected = use_signal(|| false);
let mut websocket = use_signal(|| None::<BrowserWebSocket>);
let peer_id = use_signal(|| "peer-loading...".to_string());
let remote_id = use_signal(|| String::new());
let connected = use_signal(|| false);
let websocket = use_signal(|| None::<BrowserWebSocket>);
let initiator_connection = use_signal(|| None::<RtcPeerConnection>);
let responder_connection = use_signal(|| None::<RtcPeerConnection>);
// globaler Signal für den lokal freigegebenen MediaStream (Mikrofon)

View File

@ -23,9 +23,10 @@ impl MediaManager {
}
pub fn create_peer_connection() -> Result<RtcPeerConnection, String> {
let ice_server = RtcIceServer::new();
let urls = js_sys::Array::new();
urls.push(&JsValue::from_str("stun:stun.l.google.com:19302"));
let ice_server = RtcIceServer::new();
let urls = js_sys::Array::new();
// Use centralized default STUN server constant
urls.push(&JsValue::from_str(crate::constants::DEFAULT_STUN_SERVER));
ice_server.set_urls(&urls.into());
let config = RtcConfiguration::new();
let servers = js_sys::Array::new();
@ -51,7 +52,7 @@ impl MediaManager {
.ok_or_else(|| "SDP field was not a string".to_string())?;
// 3. Init-Objekt bauen und SDP setzen
let mut init = RtcSessionDescriptionInit::new(RtcSdpType::Offer);
let init = RtcSessionDescriptionInit::new(RtcSdpType::Offer);
init.set_sdp(&sdp);
// 4. Local Description setzen
@ -112,7 +113,7 @@ impl MediaManager {
}
}
let mut init = RtcSessionDescriptionInit::new(RtcSdpType::Answer);
let init = RtcSessionDescriptionInit::new(RtcSdpType::Answer);
init.set_sdp(answer_sdp);
JsFuture::from(pc.set_remote_description(&init))