fixed webserver not working

- shared store in main
- multiple tasks for webserver
This commit is contained in:
2025-10-07 17:29:14 +02:00
parent 8cbdf834a1
commit 082f1faba9
5 changed files with 179 additions and 45 deletions

29
src/webserver/sse.rs Normal file
View File

@@ -0,0 +1,29 @@
use embassy_time::{Duration, Timer};
use log::warn;
use picoserve::response;
pub struct Events(pub TallySubscriber);
impl response::sse::EventSource for Events {
async fn write_events<W: picoserve::io::Write>(
mut self,
mut writer: response::sse::EventWriter<W>,
) -> Result<(), W::Error> {
loop {
let timeout = Timer::after(Duration::from_secs(15));
let sel = embassy_futures::select::select(self.0.next_message(), timeout);
match sel.await {
embassy_futures::select::Either::First(msg) => match msg {
embassy_sync::pubsub::WaitResult::Message(id) => {
writer.write_event("msg", id.to_string().as_str()).await?
}
embassy_sync::pubsub::WaitResult::Lagged(_) => {
warn!("SSE subscriber got lagged");
}
},
embassy_futures::select::Either::Second(_) => writer.write_keepalive().await?,
}
}
}
}