pub struct Event { /* private fields */ }
Expand description
Server-sent event
Implementations
sourceimpl Event
impl Event
sourcepub fn data<T: Into<String>>(self, data: T) -> Event
pub fn data<T: Into<String>>(self, data: T) -> Event
Set Server-sent event data
data field(s) (“data:
Examples found in repository?
More examples
examples/sse_chat.rs (line 98)
≺ ≻
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
fn user_connected(users: Users) -> impl Stream<Item = Result<Event, warp::Error>> + Send + 'static {
// Use a counter to assign a new unique ID for this user.
let my_id = NEXT_USER_ID.fetch_add(1, Ordering::Relaxed);
eprintln!("new chat user: {}", my_id);
// Use an unbounded channel to handle buffering and flushing of messages
// to the event source...
let (tx, rx) = mpsc::unbounded_channel();
let rx = UnboundedReceiverStream::new(rx);
tx.send(Message::UserId(my_id))
// rx is right above, so this cannot fail
.unwrap();
// Save the sender in our list of connected users.
users.lock().unwrap().insert(my_id, tx);
// Convert messages into Server-Sent Events and return resulting stream.
rx.map(|msg| match msg {
Message::UserId(my_id) => Ok(Event::default().event("user").data(my_id.to_string())),
Message::Reply(reply) => Ok(Event::default().data(reply)),
})
}
sourcepub fn json_data<T: Serialize>(self, data: T) -> Result<Event, Error>
pub fn json_data<T: Serialize>(self, data: T) -> Result<Event, Error>
Set Server-sent event data
data field(s) (“data:
sourcepub fn comment<T: Into<String>>(self, comment: T) -> Event
pub fn comment<T: Into<String>>(self, comment: T) -> Event
Set Server-sent event comment
Comment field (“:
Examples found in repository?
src/filters/sse.rs (line 479)
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let mut pin = self.project();
match pin.event_stream.try_poll_next(cx) {
Poll::Pending => match Pin::new(&mut pin.alive_timer).poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(_) => {
// restart timer
pin.alive_timer
.reset(tokio::time::Instant::now() + *pin.max_interval);
let comment_str = pin.comment_text.clone();
let event = Event::default().comment(comment_str);
Poll::Ready(Some(Ok(event)))
}
},
Poll::Ready(Some(Ok(event))) => {
// restart timer
pin.alive_timer
.reset(tokio::time::Instant::now() + *pin.max_interval);
Poll::Ready(Some(Ok(event)))
}
Poll::Ready(None) => Poll::Ready(None),
Poll::Ready(Some(Err(error))) => {
log::error!("sse::keep error: {}", error);
Poll::Ready(Some(Err(SseError)))
}
}
}
sourcepub fn event<T: Into<String>>(self, event: T) -> Event
pub fn event<T: Into<String>>(self, event: T) -> Event
Set Server-sent event event
Event name field (“event:
Examples found in repository?
examples/sse_chat.rs (line 98)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
fn user_connected(users: Users) -> impl Stream<Item = Result<Event, warp::Error>> + Send + 'static {
// Use a counter to assign a new unique ID for this user.
let my_id = NEXT_USER_ID.fetch_add(1, Ordering::Relaxed);
eprintln!("new chat user: {}", my_id);
// Use an unbounded channel to handle buffering and flushing of messages
// to the event source...
let (tx, rx) = mpsc::unbounded_channel();
let rx = UnboundedReceiverStream::new(rx);
tx.send(Message::UserId(my_id))
// rx is right above, so this cannot fail
.unwrap();
// Save the sender in our list of connected users.
users.lock().unwrap().insert(my_id, tx);
// Convert messages into Server-Sent Events and return resulting stream.
rx.map(|msg| match msg {
Message::UserId(my_id) => Ok(Event::default().event("user").data(my_id.to_string())),
Message::Reply(reply) => Ok(Event::default().data(reply)),
})
}
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Event
impl Send for Event
impl Sync for Event
impl Unpin for Event
impl UnwindSafe for Event
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