feat: change mpsc channels to tokio

This commit is contained in:
2026-08-22 15:36:37 +02:00
parent dd18a263d1
commit 604666b471

View File

@@ -1,4 +1,4 @@
use std::sync::mpsc::{self, Sender}; use tokio::sync::mpsc::{self, Sender};
use log::{error, info}; use log::{error, info};
use macroquad::{ use macroquad::{
@@ -62,15 +62,15 @@ impl GameClient {
return None; return None;
}; };
let (cmd_sender, cmd_recv) = mpsc::channel::<GuiCommands>(); let (cmd_sender, mut cmd_recv) = mpsc::channel::<GuiCommands>(10);
std::thread::spawn(|| { std::thread::spawn(|| {
let rt = tokio::runtime::Runtime::new().unwrap(); // i dont like this let rt = tokio::runtime::Runtime::new().unwrap(); // i dont like this
rt.spawn(async move { rt.block_on(async move {
loop { loop {
match cmd_recv.recv() { match cmd_recv.recv().await {
Ok(cmd) => match cmd { Some(cmd) => match cmd {
GuiCommands::Send(content) => { GuiCommands::Send(content) => {
client.send_text_message(content).await; client.send_text_message(content).await;
} }
@@ -86,8 +86,8 @@ impl GameClient {
GuiCommands::Move => todo!(), GuiCommands::Move => todo!(),
}, },
Err(e) => { None => {
error!("Connection broken: {}", e); error!("Connection with cmd thread broken");
return; return;
} }
} }
@@ -176,9 +176,10 @@ impl GameClient {
if before_click != self.lobby.is_ready { if before_click != self.lobby.is_ready {
if self.lobby.is_ready { if self.lobby.is_ready {
let _ = self.cmd_sender.send(GuiCommands::Ready); let _ = self.cmd_sender.try_send(GuiCommands::Ready); // TODO! Maybe handle failure
// or something
} else { } else {
let _ = self.cmd_sender.send(GuiCommands::Unready); let _ = self.cmd_sender.try_send(GuiCommands::Unready);
} }
} }
} }