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