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