cleanup and make client better

This commit is contained in:
Adam 2024-04-27 05:24:45 -04:00
parent 41015810cb
commit 998924512c

View file

@ -1,14 +1,20 @@
use futures_util::{SinkExt, StreamExt, TryFutureExt};
use std::collections::HashMap;
use std::sync::{
use std::{
collections::HashMap,
error::Error,
fs,
result::Result,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use std::{error::Error, fs, result::Result};
use tokio::sync::{mpsc, RwLock};
use tokio_stream::wrappers::UnboundedReceiverStream;
use warp::ws::{Message, WebSocket};
use warp::Filter;
use warp::{
ws::{Message, WebSocket},
Filter,
};
#[allow(non_snake_case)]
pub mod CAHd_game;
@ -125,7 +131,13 @@ async fn user_connected(ws: WebSocket, users: Users) {
let (tx, rx) = mpsc::unbounded_channel();
let mut rx = UnboundedReceiverStream::new(rx);
let _ = user_ws_tx.send(Message::text(format!("Server Message: Welcome User {}",my_id))).await;
let _ = user_ws_tx
.send(Message::text(format!(
"Server Message: Welcome User {}",
my_id
)))
.await;
tokio::task::spawn(async move {
while let Some(message) = rx.next().await {
user_ws_tx
@ -169,7 +181,7 @@ async fn user_message(my_id: usize, msg: Message, users: &Users) {
return;
};
let new_msg = format!("<User#{}>: {}", my_id, msg);
let new_msg = format!("User {}: {}", my_id, msg);
// New message from this user, send it to everyone else (except same uid)...
for (&uid, tx) in users.read().await.iter() {
@ -193,15 +205,17 @@ async fn user_disconnected(my_id: usize, users: &Users) {
static INDEX_HTML: &str = r#"<!DOCTYPE html>
<html lang="en">
<head>
<title>Warp Chat</title>
<title>Cards For Humanity Test Client</title>
</head>
<body>
<h1>Cards</h1>
<div id="chat">
<p><em>Connecting...</em></p>
</div>
<input type="text" id="text" />
<button type="button" id="send">Send</button>
<form id="muhForm" onsubmit="onSubmit();return false">
<input type="text" id="text" autocomplete="off" />
<button type="submit" id="send">Send</button>
</form>
<script type="text/javascript">
const chat = document.getElementById('chat');
const text = document.getElementById('text');
@ -226,13 +240,13 @@ static INDEX_HTML: &str = r#"<!DOCTYPE html>
chat.getElementsByTagName('em')[0].innerText = 'Disconnected!';
};
send.onclick = function() {
function onSubmit() {
const msg = text.value;
ws.send(msg);
text.value = '';
message('<You>: ' + msg);
};
message('You: ' + msg);
}
</script>
</body>
</html>