Changed initialization of WebSocket connection to OnLoad.

This commit is contained in:
ghost 2025-08-28 14:39:25 +02:00
parent 18a26c6bf1
commit 41affe1aaa
3 changed files with 56 additions and 12 deletions

17
.gitignore vendored
View File

@ -1,7 +1,22 @@
# Generated by Cargo # Generated by Cargo
# will have compiled files and executables # will have compiled files and executables
/target debug
target
.DS_Store .DS_Store
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Generated by cargo mutants
# Contains mutation testing data
**/mutants.out*/
# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

View File

@ -17,6 +17,7 @@ js-sys = "0.3.61"
# web-sys with features for media devices # web-sys with features for media devices
web-sys = { version = "0.3.77", features = [ web-sys = { version = "0.3.77", features = [
"BinaryType",
"Navigator", "Navigator",
"MediaDevices", "MediaDevices",
"MediaStream", "MediaStream",

View File

@ -3,6 +3,10 @@
mod utils; mod utils;
use dioxus::{html::{g::media, h3}, prelude::*}; use dioxus::{html::{g::media, h3}, prelude::*};
use wasm_bindgen::JsCast;
use wasm_bindgen::prelude::Closure;
use web_sys::{BinaryType, MessageEvent, WebSocket as BrowserWebSocket};
use utils::{MediaManager, MediaState}; use utils::{MediaManager, MediaState};
const FAVICON: Asset = asset!("/assets/favicon.ico"); const FAVICON: Asset = asset!("/assets/favicon.ico");
@ -62,11 +66,7 @@ pub fn Content() ->Element {
ConnectionPanel { ConnectionPanel {
connected: connected.clone(), connected: connected.clone(),
local_peer_id: local_peer_id.clone(), local_peer_id: local_peer_id.clone(),
remote_peer_id: remote_peer_id.clone(), remote_peer_id: remote_peer_id.clone()
on_connect: move |_| {
log::info!("Verbindung wird hergestellt...");
connected.set(true);
}
} }
// Call Controls // Call Controls
@ -106,9 +106,35 @@ pub fn Content() ->Element {
fn ConnectionPanel( fn ConnectionPanel(
connected: Signal<bool>, connected: Signal<bool>,
local_peer_id: Signal<String>, local_peer_id: Signal<String>,
mut remote_peer_id: Signal<String>, remote_peer_id: Signal<String>
on_connect: EventHandler<MouseEvent>
) -> Element { ) -> Element {
// Websocket signal
let ws = use_signal(|| None::<BrowserWebSocket>);
// One-time initialization of WebSocket connection
use_effect(move || {
to_owned![ws];
if ws.read().is_none() {
// Create new WebSocket connection
if let Ok(socket) = BrowserWebSocket::new("ws://localhost:8080/ws") {
socket.set_binary_type(BinaryType::Arraybuffer);
ws.write().replace(socket.clone());
// Event handler
let onmessage = Closure::wrap(Box::new(move |e: MessageEvent| {
if let Some(text) = e.data().as_string() {
// Hier später SPD/Candidate verarbeiten
log::info!("WS empfangen: {}", text);
}
}) as Box<dyn FnMut(MessageEvent)>);
socket.set_onmessage(Some(onmessage.as_ref().unchecked_ref()));
onmessage.forget(); // Verhindert, dass Closure gelöscht wird
}
}
});
rsx! { rsx! {
div { div {
class: "connection-panel", class: "connection-panel",
@ -151,10 +177,12 @@ fn ConnectionPanel(
button { button {
class: "connect-btn", class: "connect-btn",
onclick: on_connect, disabled: ws.read().is_some(),
disabled: connected(), onclick: move |_| {
if connected() { connected.set(true);
"Verbunden" },
if ws.read().is_some() {
"✅ Verbunden"
} }
else { else {
"Mit Signaling Server verbinden" "Mit Signaling Server verbinden"