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="msgs"></div>
<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" />
</form>
</body>

View File

@ -41,5 +41,7 @@ ws.onmessage = (event) => {
document.getElementById("send").onsubmit = (event) => {
event.preventDefault();
// 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;
pub const USERS_LEN: u8 = 4;
const MSG_MAX_SIZE: usize = 10;
const MSG_MAX_SIZE: usize = 500;
#[derive(Debug, Serialize)]
struct Msg<'a> {
id: usize,
author: u8,
content: &'a str,
}
// {"id"=999999,"author"="","content"=""}
const MSGS_SIZE: usize = 30;
#[derive(Debug)]
struct Msgs {
/// 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.
/// No message is splitted
/// * 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.
/// * No message is splitted
inner: [u8; MSGS_SIZE],
/// next byte index
head: usize,
@ -53,7 +52,7 @@ impl Msgs {
self.head += 3;
self.next_msg += 1;
}
// Iter messages from present to past
/// Iter messages from present to past
fn iter(&self) -> MsgsIter {
if self.head == 0 {
MsgsIter {
@ -181,7 +180,6 @@ impl App for ChatApp {
}
fn accept_ws(&self, path: &str) -> bool {
path == "/"
// path.len() > 1 && path.len() <= 17
}
async fn handle_ws<'a, const BUF_SIZE: usize>(
&'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 {
match id {
0 => "0",