1
0
mirror of https://github.com/pcvolkmer/arsnova-client.git synced 2025-04-19 19:16:51 +00:00

refactor: select next incoming message or ping message due

This commit is contained in:
Paul-Christian Volkmer 2023-12-23 13:44:21 +01:00
parent 11cb5101a8
commit c7469fc320
2 changed files with 28 additions and 30 deletions

View File

@ -51,7 +51,7 @@ pub struct Cli {
url: String, url: String,
} }
#[tokio::main(worker_threads = 3)] #[tokio::main(worker_threads = 2)]
async fn main() -> Result<(), ()> { async fn main() -> Result<(), ()> {
let cli = Cli::parse(); let cli = Cli::parse();

View File

@ -28,8 +28,8 @@ use futures_util::{SinkExt, StreamExt};
use reqwest::{IntoUrl, StatusCode}; use reqwest::{IntoUrl, StatusCode};
use serde::Deserialize; use serde::Deserialize;
use serde_json::json; use serde_json::json;
use tokio::select;
use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::mpsc::{Receiver, Sender};
use tokio::{join, select};
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::Message; use tokio_tungstenite::tungstenite::Message;
use url::Url; use url::Url;
@ -493,7 +493,7 @@ impl Client<LoggedIn> {
.await .await
.map_err(|_| ConnectionError)?; .map_err(|_| ConnectionError)?;
let (mut write, read) = socket.split(); let (mut write, mut read) = socket.split();
if write if write
.send(Message::Text( .send(Message::Text(
@ -508,12 +508,11 @@ impl Client<LoggedIn> {
)) ))
.await .await
{ {
Ok(_) => {} Ok(_) => loop {
Err(_) => return Err(ConnectionError), select! {
} Some(next) = read.next() => {
match &next {
let jh1 = read.for_each(|msg| async { Ok(msg) => {
if let Ok(msg) = msg {
if msg.is_text() && msg.clone().into_text().unwrap().starts_with("MESSAGE") { if msg.is_text() && msg.clone().into_text().unwrap().starts_with("MESSAGE") {
if let Ok(msg) = WsFeedbackMessage::parse(msg.to_text().unwrap()) { if let Ok(msg) = WsFeedbackMessage::parse(msg.to_text().unwrap()) {
if msg.body.body_type == "FeedbackChanged" { if msg.body.body_type == "FeedbackChanged" {
@ -528,17 +527,16 @@ impl Client<LoggedIn> {
} }
} }
} }
}); Err(_) => break
}
let jh2 = tokio::spawn(async move { }
loop { _ = tokio::time::sleep(Duration::from_secs(15)) => {
tokio::time::sleep(Duration::from_secs(15)).await;
let _ = write.send(Message::Text("\n".to_string())).await; let _ = write.send(Message::Text("\n".to_string())).await;
} }
}); }
},
let _ = join!(jh1, jh2); Err(_) => return Err(ConnectionError),
return Ok(()); }
} }
Err(ConnectionError) Err(ConnectionError)