refactor: move websocket into existing webserver and folder structure

keeps things consistent and avoids having 2 webservers running under actix, can be reverted if its not desirable however i think this is the best option
This commit is contained in:
Radical 2025-05-15 11:57:47 +00:00
parent 79cfa25855
commit cb3c1ee6e4
4 changed files with 41 additions and 50 deletions

View file

@ -1,4 +1,5 @@
pub mod messages;
pub mod socket;
use actix_web::{delete, get, web, Error, HttpRequest, HttpResponse};
use crate::{api::v1::auth::check_access_token, structs::{Channel, Member}, utils::get_auth_header, Data};

View file

@ -0,0 +1,41 @@
use actix_web::{get, rt, web, Error, HttpRequest, HttpResponse};
use actix_ws::AggregatedMessage;
use futures_util::StreamExt as _;
#[get("{uuid}/channels/{channel_uuid}/socket")]
pub async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
let (res, mut session, stream) = actix_ws::handle(&req, stream)?;
let mut stream = stream
.aggregate_continuations()
// aggregate continuation frames up to 1MiB
.max_continuation_size(2_usize.pow(20));
// start task but don't wait for it
rt::spawn(async move {
// receive messages from websocket
while let Some(msg) = stream.next().await {
match msg {
Ok(AggregatedMessage::Text(text)) => {
// echo text message
session.text(text).await.unwrap();
}
Ok(AggregatedMessage::Binary(bin)) => {
// echo binary message
session.binary(bin).await.unwrap();
}
Ok(AggregatedMessage::Ping(msg)) => {
// respond to PING frame with PONG frame
session.pong(&msg).await.unwrap();
}
_ => {}
}
}
});
// respond immediately with response connected to WS session
Ok(res)
}

View file

@ -17,6 +17,7 @@ pub fn web() -> Scope {
.service(channels::uuid::get)
.service(channels::uuid::delete)
.service(channels::uuid::messages::get)
.service(channels::uuid::socket::echo)
// Roles
.service(roles::get)
.service(roles::create)