This commit is contained in:
2026-09-21 15:19:13 +02:00
parent 7f1402dd33
commit 73233e9b57
6 changed files with 71 additions and 53 deletions
+12 -8
View File
@@ -1,12 +1,10 @@
// This is the first point when WebRTC ask for a connection
use super::client::RtcClient;
use super::client::RTCClient;
use crate::core::AppState;
use crate::models::{channel, user};
use axum::extract::ws::{Message, WebSocket};
use futures_util::{SinkExt, StreamExt};
use rustrtc::PeerConnection;
use std::sync::Arc;
use tokio::sync::mpsc;
// 1. Le frontend ouvre /rtc/{channel_id}
@@ -31,10 +29,11 @@ pub async fn ws_entrypoint_handler(
let (mut sender, mut receiver) = socket.split();
let (tx, mut rx) = mpsc::unbounded_channel::<Message>();
let peer_connection = Arc::new(PeerConnection::new(state.rtc.config.clone()));
let mut rtc_client = RtcClient::new(user, channel, peer_connection, tx);
let peer_connection = state.rtc.new_peer_connection();
let mut rtc_client = RTCClient::new(user, channel, peer_connection, tx);
let send_task = tokio::spawn(async move {
// Task pour envoyer les message au frontend depuis RTCClient
let mut send_task = tokio::spawn(async move {
while let Some(message) = rx.recv().await {
if sender.send(message).await.is_err() {
break;
@@ -42,11 +41,16 @@ pub async fn ws_entrypoint_handler(
}
});
// Task pour recevoir les messages du frontend afin de les transmettre à RTCClient
let client_clone = rtc_client.clone();
let state_clone = state.clone();
let mut recv_task = tokio::spawn(async move {
while let Some(Ok(message)) = receiver.next().await {
client_clone.on_message(message, &state_clone).await;
client_clone.on_message(message).await;
}
});
tokio::select! {
_ = (&mut send_task) => recv_task.abort(),
_ = (&mut recv_task) => send_task.abort(),
};
}