This commit is contained in:
2026-09-19 21:34:06 +02:00
parent 8d48b1617b
commit 8297111060
8 changed files with 97 additions and 23 deletions
+12 -3
View File
@@ -1,13 +1,22 @@
use crate::core::AppState;
use crate::http::context::CurrentUser;
use crate::http::error::HTTPError;
use crate::rtc::ws_entrypoint::ws_entrypoint_handler;
use axum::extract::{State, WebSocketUpgrade};
use axum::extract::{Path, State, WebSocketUpgrade};
use axum::response::IntoResponse;
use uuid::Uuid;
pub async fn ws_handler(
ws: WebSocketUpgrade,
Path(channel_id): Path<Uuid>,
State(state): State<AppState>,
CurrentUser(user): CurrentUser,
) -> impl IntoResponse {
ws.on_upgrade(move |socket| ws_entrypoint_handler(socket, state, user))
) -> Result<impl IntoResponse, HTTPError> {
let channel = state
.repositories
.channel
.get_by_id(channel_id)
.await?
.ok_or(HTTPError::NotFound)?;
Ok(ws.on_upgrade(move |socket| ws_entrypoint_handler(socket, state, channel, user)))
}
+1 -1
View File
@@ -4,5 +4,5 @@ use axum::Router;
use axum::routing::get;
pub fn router() -> Router<AppState> {
Router::new().route("/rtc", get(handlers::ws_handler))
Router::new().route("/rtc/{channel_id}", get(handlers::ws_handler))
}
+35
View File
@@ -0,0 +1,35 @@
use crate::models::{channel, user};
use axum::extract::ws::Message;
use rustrtc::peer_connection::PeerConnection;
use std::sync::Arc;
use tokio::sync::mpsc::UnboundedSender;
/// Client connecté à un canal RTC.
#[derive(Clone)]
pub struct RtcClient {
pub user: user::Model,
pub channel: channel::Model,
pub peer_connection: Arc<PeerConnection>,
pub websocket_sender: UnboundedSender<Message>,
}
impl RtcClient {
pub fn new(
user: user::Model,
channel: channel::Model,
peer_connection: Arc<PeerConnection>,
websocket_sender: UnboundedSender<Message>,
) -> Self {
Self {
user,
channel,
peer_connection,
websocket_sender,
}
}
/// Ferme proprement la connexion WebRTC.
pub fn close(&self) {
self.peer_connection.close();
}
}
+1
View File
@@ -1,3 +1,4 @@
mod client;
mod metrics;
pub mod ws_entrypoint;
+32 -3
View File
@@ -1,7 +1,36 @@
// This is the first point when WebRTC ask for a connection
use super::client::RtcClient;
use crate::core::AppState;
use crate::models::user;
use axum::extract::ws::WebSocket;
use crate::models::{channel, user};
use axum::extract::ws::{Message, WebSocket};
use futures_util::{SinkExt, StreamExt};
use tokio::sync::mpsc;
pub async fn ws_entrypoint_handler(socket: WebSocket, state: AppState, user: user::Model) {}
pub async fn ws_entrypoint_handler(
socket: WebSocket,
state: AppState,
channel: channel::Model,
user: user::Model,
) {
let (mut sender, mut receiver) = socket.split();
let (tx, mut rx) = mpsc::unbounded_channel::<Message>();
// todo : faire le peer connexion.
let mut rtc_client = RtcClient::new(user, channel, peer_connection, tx);
let send_task = tokio::spawn(async move {
while let Some(message) = rx.recv().await {
if sender.send(message).await.is_err() {
break;
}
}
});
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;
}
});
}