pub struct KeepAlive { /* private fields */ }
Expand description
Configure the interval between keep-alive messages, the content of each message, and the associated stream.
Implementations
sourceimpl KeepAlive
impl KeepAlive
sourcepub fn interval(self, time: Duration) -> Self
pub fn interval(self, time: Duration) -> Self
Customize the interval between keep-alive messages.
Default is 15 seconds.
sourcepub fn text(self, text: impl Into<Cow<'static, str>>) -> Self
pub fn text(self, text: impl Into<Cow<'static, str>>) -> Self
Customize the text of the keep-alive message.
Default is an empty comment.
sourcepub fn stream<S>(
self,
event_stream: S
) -> impl TryStream<Ok = Event, Error = impl StdError + Send + Sync + 'static> + Send + 'static where
S: TryStream<Ok = Event> + Send + 'static,
S::Error: StdError + Send + Sync + 'static,
pub fn stream<S>(
self,
event_stream: S
) -> impl TryStream<Ok = Event, Error = impl StdError + Send + Sync + 'static> + Send + 'static where
S: TryStream<Ok = Event> + Send + 'static,
S::Error: StdError + Send + Sync + 'static,
Wrap an event stream with keep-alive functionality.
See keep_alive
for more.
Examples found in repository?
examples/sse_chat.rs (line 43)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
async fn main() {
pretty_env_logger::init();
// Keep track of all connected users, key is usize, value
// is an event stream sender.
let users = Arc::new(Mutex::new(HashMap::new()));
// Turn our "state" into a new Filter...
let users = warp::any().map(move || users.clone());
// POST /chat -> send message
let chat_send = warp::path("chat")
.and(warp::post())
.and(warp::path::param::<usize>())
.and(warp::body::content_length_limit(500))
.and(
warp::body::bytes().and_then(|body: bytes::Bytes| async move {
std::str::from_utf8(&body)
.map(String::from)
.map_err(|_e| warp::reject::custom(NotUtf8))
}),
)
.and(users.clone())
.map(|my_id, msg, users| {
user_message(my_id, msg, &users);
warp::reply()
});
// GET /chat -> messages stream
let chat_recv = warp::path("chat").and(warp::get()).and(users).map(|users| {
// reply using server-sent events
let stream = user_connected(users);
warp::sse::reply(warp::sse::keep_alive().stream(stream))
});
// GET / -> index html
let index = warp::path::end().map(|| {
warp::http::Response::builder()
.header("content-type", "text/html; charset=utf-8")
.body(INDEX_HTML)
});
let routes = index.or(chat_recv).or(chat_send);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for KeepAlive
impl Send for KeepAlive
impl Sync for KeepAlive
impl Unpin for KeepAlive
impl UnwindSafe for KeepAlive
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self