chat works
This commit is contained in:
parent
82c86bba16
commit
f523ec812c
@ -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>\
|
||||
<input \
|
||||
id=\"msg\" \
|
||||
name=\"msg\" \
|
||||
maxlength=\"{}\" \
|
||||
<form id=\"send-message\" \
|
||||
hx-post=\"/chat/send\" \
|
||||
hx-include=\"#username\" \
|
||||
hx-target=\"#messages\" \
|
||||
hx-swap=\"beforeend\"\
|
||||
>",
|
||||
hx-target=\"this\" \
|
||||
hx-swap=\"innerHTML\"\
|
||||
>\
|
||||
<input \
|
||||
id=\"msg\" \
|
||||
name=\"msg\" \
|
||||
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;
|
||||
}
|
||||
info!("msg_id:{} | next:{}", msg_id, msgs.next);
|
||||
if msg_id == msgs.next {
|
||||
info!("test1");
|
||||
if poll {
|
||||
return (HttpResCode::NoContent, "", &[]);
|
||||
}
|
||||
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\"",
|
||||
|
Loading…
Reference in New Issue
Block a user