chat works

This commit is contained in:
Arkitu 2025-05-04 11:59:10 +02:00
parent 82c86bba16
commit f523ec812c

View File

@ -47,6 +47,7 @@ impl App for ChatApp {
let mut load = None;
let mut username = None;
let mut msg_content = None;
let mut poll = false;
for arg in args.split('&').chain(content.split('&')) {
match arg.split_once('=') {
Some(("load", n)) => {
@ -79,6 +80,7 @@ impl App for ChatApp {
}
msg_content = Some(msg);
}
Some(("poll", "true")) => poll = true,
_ => {}
}
}
@ -100,15 +102,19 @@ impl App for ChatApp {
hx-trigger=\"load\" \
></div>\
</div>\
<form id=\"send-message\" \
hx-post=\"/chat/send\" \
hx-include=\"#username\" \
hx-target=\"this\" \
hx-swap=\"innerHTML\"\
>\
<input \
id=\"msg\" \
name=\"msg\" \
maxlength=\"{}\" \
hx-post=\"/chat/send\" \
hx-include=\"#username\" \
hx-target=\"#messages\" \
hx-swap=\"beforeend\"\
>",
maxlength=\"{}\"\
>\
<button>Send</button>
</form>",
username.unwrap(),
MEMORY_SIZE,
MAX_MESSAGE_SIZE
@ -121,7 +127,19 @@ impl App for ChatApp {
author: username.unwrap(),
content: msg_content.unwrap(),
});
return (HttpResCode::NoContent, "", &[]);
self.res_buf.clear();
unwrap(write!(
&mut self.res_buf,
"<input \
id=\"msg\" \
name=\"msg\" \
maxlength=\"{}\"\
>\
<button>Send</button>",
MAX_MESSAGE_SIZE
))
.await;
return (HttpResCode::Ok, "html", &self.res_buf);
} else if path.starts_with("/chat/message/") && path.len() > 14 {
let msg_id: u16 = match path[14..].parse() {
Ok(n) => n,
@ -137,14 +155,13 @@ impl App for ChatApp {
self.res_buf.clear();
unwrap(write!(&mut self.res_buf, "<div class=\"message\"")).await;
if msg_id == msgs.next {
unwrap(write!(&mut self.res_buf, " style=\"display: none;\"")).await;
if poll {
return (HttpResCode::NoContent, "", &[]);
}
info!("msg_id:{} | next:{}", msg_id, msgs.next);
if msg_id == msgs.next {
info!("test1");
unwrap(write!(
&mut self.res_buf,
" hx-get=\"/chat/message/{}?load={}\" \
" style=\"display: none;\" \
hx-get=\"/chat/message/{}?load={}&poll=true\" \
hx-target=\"this\" \
hx-swap=\"outerHTML\" \
hx-trigger=\"every 100ms\"",