mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 00:15:17 +00:00
Handle Websocket continuation messages
This commit is contained in:
@ -14,8 +14,12 @@ use crate::data::error::{ExecError, Res};
|
||||
use crate::helpers::events_helper;
|
||||
use crate::helpers::events_helper::Event;
|
||||
use crate::utils::network_utils::match_ip;
|
||||
use actix_http::ws::Item;
|
||||
|
||||
struct RtcRelayActor {}
|
||||
#[derive(Default)]
|
||||
struct RtcRelayActor {
|
||||
recv_buff: Vec<u8>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Deserialize)]
|
||||
@ -105,6 +109,22 @@ impl actix::Actor for RtcRelayActor {
|
||||
}
|
||||
}
|
||||
|
||||
impl RtcRelayActor {
|
||||
fn handle_message(&self, txt: &str) {
|
||||
match serde_json::from_str::<RTCSocketMessage>(&txt) {
|
||||
Err(e) => {
|
||||
eprintln!("Failed to parse a message from RTC proxy! {:#?}", e);
|
||||
}
|
||||
|
||||
Ok(msg) => {
|
||||
if let Err(e) = process_message_from_relay(&msg) {
|
||||
eprintln!("Failed to process signal from RTC Relay! {:#?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamHandler<Result<actix_web_actors::ws::Message, actix_web_actors::ws::ProtocolError>> for RtcRelayActor {
|
||||
fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
|
||||
let msg = match msg {
|
||||
@ -122,24 +142,31 @@ impl StreamHandler<Result<actix_web_actors::ws::Message, actix_web_actors::ws::P
|
||||
// Handle messages
|
||||
match msg {
|
||||
Message::Text(txt) => {
|
||||
match serde_json::from_str::<RTCSocketMessage>(&txt) {
|
||||
Err(e) => {
|
||||
eprintln!("Failed to parse a message from RTC proxy! {:#?}", e);
|
||||
}
|
||||
|
||||
Ok(msg) => {
|
||||
if let Err(e) = process_message_from_relay(&msg) {
|
||||
eprintln!("Failed to process signal from RTC Relay! {:#?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.handle_message(&txt);
|
||||
}
|
||||
Message::Binary(_) => {
|
||||
eprintln!("RTC WS Message::Binary");
|
||||
ctx.stop();
|
||||
}
|
||||
Message::Continuation(_) => {
|
||||
eprintln!("RTC WS Message::Continuation");
|
||||
Message::Continuation(c) => {
|
||||
eprintln!("RTC WS Message::Continuation {:?}", c);
|
||||
|
||||
match c {
|
||||
Item::FirstText(c) => {
|
||||
self.recv_buff = c.as_ref().to_vec();
|
||||
}
|
||||
Item::FirstBinary(_) => {
|
||||
eprintln!("Received binary !!!");
|
||||
ctx.stop();
|
||||
}
|
||||
Item::Continue(c) => {
|
||||
self.recv_buff.extend_from_slice(c.as_ref());
|
||||
}
|
||||
Item::Last(c) => {
|
||||
self.recv_buff.extend_from_slice(c.as_ref());
|
||||
self.handle_message(&String::from_utf8_lossy(&self.recv_buff))
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::Ping(data) => {
|
||||
ctx.pong(&data);
|
||||
@ -262,7 +289,7 @@ pub async fn open_ws(req: actix_web::HttpRequest,
|
||||
}
|
||||
|
||||
// Start the actor
|
||||
actix_web_actors::ws::start(RtcRelayActor {}, &req, stream)
|
||||
actix_web_actors::ws::start(RtcRelayActor::default(), &req, stream)
|
||||
}
|
||||
|
||||
/// Send a message to the relay
|
||||
|
Reference in New Issue
Block a user