This commit is contained in:
Arkitu 2025-08-20 14:07:31 +02:00
parent 078734e8a1
commit 47e8ba586c
3 changed files with 16 additions and 14 deletions

View File

@ -8,7 +8,14 @@
<div id="users"></div> <div id="users"></div>
<div id="msgs"></div> <div id="msgs"></div>
<form id="send"> <form id="send">
<input id="sendcontent" name="content" autofocus required /> <input
id="sendcontent"
name="content"
autofocus
required
minlength="1"
maxlength="500"
/>
<input type="submit" value="Send" /> <input type="submit" value="Send" />
</form> </form>
</body> </body>

View File

@ -41,5 +41,7 @@ ws.onmessage = (event) => {
document.getElementById("send").onsubmit = (event) => { document.getElementById("send").onsubmit = (event) => {
event.preventDefault(); event.preventDefault();
// console.log(event, document.getElementById("sendcontent").value); // console.log(event, document.getElementById("sendcontent").value);
ws.send("send " + document.getElementById("sendcontent").value); let content = document.getElementById("sendcontent");
ws.send("send " + content.value);
content.value = "";
}; };

View File

@ -13,21 +13,20 @@ use crate::{apps::App, socket::ws::WsMsg};
// Must be <= u8::MAX-1; // Must be <= u8::MAX-1;
pub const USERS_LEN: u8 = 4; pub const USERS_LEN: u8 = 4;
const MSG_MAX_SIZE: usize = 10; const MSG_MAX_SIZE: usize = 500;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct Msg<'a> { struct Msg<'a> {
id: usize, id: usize,
author: u8, author: u8,
content: &'a str, content: &'a str,
} }
// {"id"=999999,"author"="","content"=""}
const MSGS_SIZE: usize = 30; const MSGS_SIZE: usize = 30;
#[derive(Debug)] #[derive(Debug)]
struct Msgs { struct Msgs {
/// Memory layout with sizes in bytes : ...|content: len|len: 2|author+1: 1|... /// * Memory layout with sizes in bytes : ...|content: len|len: 2|author+1: 1|...
/// `author=0` means theres no message, it's just padding and should be skipped. /// * `author=0` means theres no message, it's just padding and should be skipped.
/// No message is splitted /// * No message is splitted
inner: [u8; MSGS_SIZE], inner: [u8; MSGS_SIZE],
/// next byte index /// next byte index
head: usize, head: usize,
@ -53,7 +52,7 @@ impl Msgs {
self.head += 3; self.head += 3;
self.next_msg += 1; self.next_msg += 1;
} }
// Iter messages from present to past /// Iter messages from present to past
fn iter(&self) -> MsgsIter { fn iter(&self) -> MsgsIter {
if self.head == 0 { if self.head == 0 {
MsgsIter { MsgsIter {
@ -181,7 +180,6 @@ impl App for ChatApp {
} }
fn accept_ws(&self, path: &str) -> bool { fn accept_ws(&self, path: &str) -> bool {
path == "/" path == "/"
// path.len() > 1 && path.len() <= 17
} }
async fn handle_ws<'a, const BUF_SIZE: usize>( async fn handle_ws<'a, const BUF_SIZE: usize>(
&'a mut self, &'a mut self,
@ -237,11 +235,6 @@ impl App for ChatApp {
} }
} }
// #[derive(Deserialize)]
// enum ClientMsg {
// ReqMsg(usize),
// }
pub async fn id_to_static_str(id: u8) -> &'static str { pub async fn id_to_static_str(id: u8) -> &'static str {
match id { match id {
0 => "0", 0 => "0",