made ApplicationMessage cloneable
This commit is contained in:
parent
1e37189866
commit
e9b4bfee32
1 changed files with 48 additions and 36 deletions
84
src/lib.rs
84
src/lib.rs
|
@ -3,7 +3,7 @@ use std::sync::atomic::AtomicBool;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use async_channel::{Receiver, Sender};
|
use async_channel::{Receiver, Sender};
|
||||||
use futures_util::stream::{SplitStream, StreamExt};
|
use futures_util::stream::{SplitSink, SplitStream, StreamExt};
|
||||||
use futures_util::SinkExt;
|
use futures_util::SinkExt;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
|
@ -18,7 +18,7 @@ use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;
|
||||||
|
|
||||||
static MAX_MESSAGE: usize = 50;
|
static MAX_MESSAGE: usize = 50;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
pub enum ApplicationMessage {
|
pub enum ApplicationMessage {
|
||||||
WebRTCPacket(RTCSessionDescription),
|
WebRTCPacket(RTCSessionDescription),
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ pub async fn connect_to_server(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut ws_sender, ws_recv) = ws.split();
|
let (ws_sender, ws_recv) = ws.split();
|
||||||
|
|
||||||
let to_app: AppSender = to_app_events;
|
let to_app: AppSender = to_app_events;
|
||||||
let receiver_is_closed = Arc::new(AtomicBool::new(false));
|
let receiver_is_closed = Arc::new(AtomicBool::new(false));
|
||||||
|
@ -70,45 +70,57 @@ pub async fn connect_to_server(
|
||||||
listen_to_ws(to_app, moved_copy, ws_recv)
|
listen_to_ws(to_app, moved_copy, ws_recv)
|
||||||
});
|
});
|
||||||
|
|
||||||
while let Ok(msg) = to_core_reciever.recv().await {
|
tokio::spawn(async move {
|
||||||
#[cfg(debug_assertions)]
|
send_to_wc(to_core_reciever, receiver_is_closed, ws_sender)
|
||||||
{
|
});
|
||||||
// serialized message
|
|
||||||
match serde_json::to_string(&msg) {
|
|
||||||
Err(e) => error!("Could not serialize ApplicationMessage to JSON! {e}"),
|
|
||||||
Ok(msg) => {
|
|
||||||
if let Err(e) = ws_sender.send(Message::text(msg)).await {
|
|
||||||
error!("Could not send text ApplicationMessage to websocket! Closing websocket\n{e}");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
{
|
|
||||||
match bincode::serialize(&msg) {
|
|
||||||
Err(e) => error!("Could not serialize ApplicationMessage into binary! {e}"),
|
|
||||||
Ok(e) => {
|
|
||||||
if let Err(e) = ws_sender.send(Message::binary(msg)).await {
|
|
||||||
error!("Could not send binary ApplicationMessage to websocket! Closing websocket\n{e}");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = ws_sender.close().await;
|
|
||||||
|
|
||||||
info!("Websocket connect successfully");
|
info!("Websocket connect successfully");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip_all)]
|
||||||
|
async fn send_to_wc(
|
||||||
|
from_app: AppReceiver,
|
||||||
|
is_closed: Arc<AtomicBool>,
|
||||||
|
mut sender: SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, tokio_tungstenite::tungstenite::Message>
|
||||||
|
) {
|
||||||
|
while let Ok(msg) = from_app.recv().await {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
// serialized message
|
||||||
|
match serde_json::to_string(&msg) {
|
||||||
|
Err(e) => error!("Could not serialize ApplicationMessage to JSON! {e}"),
|
||||||
|
Ok(msg) => {
|
||||||
|
if let Err(e) = sender.send(Message::text(msg)).await {
|
||||||
|
error!("Could not send text ApplicationMessage to websocket! Closing websocket\n{e}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
{
|
||||||
|
match bincode::serialize(&msg) {
|
||||||
|
Err(e) => error!("Could not serialize ApplicationMessage into binary! {e}"),
|
||||||
|
Ok(e) => {
|
||||||
|
if let Err(e) = sender.send(Message::binary(msg)).await {
|
||||||
|
error!("Could not send binary ApplicationMessage to websocket! Closing websocket\n{e}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = sender.close().await;
|
||||||
|
is_closed.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
async fn listen_to_ws(
|
async fn listen_to_ws(
|
||||||
|
|
Loading…
Reference in a new issue