OPENWORM GAMAING (can send messages to server)
This commit is contained in:
@@ -7,6 +7,7 @@ use std::{
|
||||
str::FromStr,
|
||||
sync::Arc,
|
||||
};
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
|
||||
pub fn connect(handle: AppHandle) {
|
||||
std::thread::spawn(|| {
|
||||
@@ -14,6 +15,12 @@ pub fn connect(handle: AppHandle) {
|
||||
});
|
||||
}
|
||||
|
||||
pub enum NetCmd {
|
||||
SendMsg(String),
|
||||
}
|
||||
|
||||
pub type NetSender = UnboundedSender<NetCmd>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn connect_the(handle: AppHandle) -> anyhow::Result<()> {
|
||||
let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
|
||||
@@ -43,18 +50,26 @@ async fn connect_the(handle: AppHandle) -> anyhow::Result<()> {
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to connect: {}", e))?;
|
||||
|
||||
let (mut send, recv) = conn
|
||||
.open_bi()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
|
||||
let (client_send, mut client_recv) = tokio::sync::mpsc::unbounded_channel::<NetCmd>();
|
||||
|
||||
drop(recv);
|
||||
handle.send(ClientEvent::Connect(client_send));
|
||||
|
||||
handle.send(ClientEvent::Connect);
|
||||
while let Some(msg) = client_recv.recv().await {
|
||||
match msg {
|
||||
NetCmd::SendMsg(content) => {
|
||||
let (mut send, recv) = conn
|
||||
.open_bi()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
|
||||
|
||||
send.write_all(&[39]).await.expect("failed to send");
|
||||
send.finish().unwrap();
|
||||
send.stopped().await.unwrap();
|
||||
drop(recv);
|
||||
|
||||
send.write_all(content.as_bytes()).await.expect("failed to send");
|
||||
send.finish().unwrap();
|
||||
send.stopped().await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user