send explicit pings to client
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -1,12 +1,14 @@
|
||||
use std::convert::Infallible;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use futures_util::StreamExt;
|
||||
use futures_util::{stream, StreamExt};
|
||||
#[allow(unused_imports)] use log::{debug, error, info, trace};
|
||||
use rppal::gpio::{Gpio, Level, Trigger};
|
||||
use smallvec::SmallVec;
|
||||
use tokio::sync::mpsc;
|
||||
use warp::{Filter, sse};
|
||||
use tokio::{ sync::mpsc,
|
||||
time::interval };
|
||||
use warp::{Filter, sse, sse::ServerSentEvent};
|
||||
|
||||
|
||||
const BUTTON_PIN: u8 = 26;
|
||||
@@ -51,11 +53,16 @@ async fn main() {
|
||||
let (tx, rx) = mpsc::channel(1);
|
||||
clients.lock().unwrap().push(tx);
|
||||
|
||||
let stream = rx.map(|()| {
|
||||
debug!("sending sse");
|
||||
Ok::<_, Infallible>((sse::event("ring"),
|
||||
sse::data("")))
|
||||
});
|
||||
let stream = stream::select(
|
||||
interval(Duration::from_secs(5)).map(move |_| {
|
||||
Ok::<_, Infallible>((sse::event("ping"), sse::data("")).into_a())
|
||||
}),
|
||||
|
||||
rx.map(|()| {
|
||||
debug!("sending ring sse");
|
||||
Ok::<_, Infallible>((sse::event("ring"), sse::data("")).into_b())
|
||||
})
|
||||
);
|
||||
sse::reply(stream)
|
||||
});
|
||||
|
||||
|
||||
@@ -8,12 +8,19 @@
|
||||
<section id="log">
|
||||
<h1>Incoming Events</h1>
|
||||
</section>
|
||||
<template id="logline">
|
||||
<p>Ping
|
||||
</template>
|
||||
<template id="ring">
|
||||
<p>Ring!
|
||||
</template>
|
||||
<script>
|
||||
"use strict";
|
||||
let sse = new EventSource('events');
|
||||
sse.addEventListener('ping', msg => {
|
||||
const template = document.querySelector('#logline').content.cloneNode(true);
|
||||
document.querySelector('#log').appendChild(template);
|
||||
});
|
||||
sse.addEventListener('ring', msg => {
|
||||
const template = document.querySelector('#ring').content.cloneNode(true);
|
||||
document.querySelector('#log').appendChild(template);
|
||||
|
||||
Reference in New Issue
Block a user