[][src]Struct localghost::net::EventSource

pub struct EventSource { /* fields omitted */ }

A receiver of Server Sent Events (SSE).

Examples

use localghost::dom::{self, Element};
use localghost::prelude::*;
use localghost::net::EventSource;
use async_std::prelude::*;

use std::io;

#[localghost::main]
async fn main() -> io::Result<()> {
    // Connect the `EventSource`.
    let interests = ["fruit"];
    let mut sse = EventSource::connect("http://localhost:8081/sse", &interests).await?;

    // Create a table
    let table = Element::new("table");
    let tr = Element::new("tr");
    tr.append(Element::with_text("th", "name"));
    tr.append(Element::with_text("th", "data"));
    table.append(tr);
    dom::body().append(&table);

    // For every event in the `EventSource` add an entry to the table.
    while let Some(ev) = sse.next().await.transpose()? {
        let tr = Element::new("tr");
        tr.append(Element::with_text("td", ev.name()));
        tr.append(Element::with_text("td", ev.data()));
        table.append(tr);
    };

    Ok(())
}

Implementations

impl EventSource[src]

pub async fn connect<S, '_, '_>(
    url: &'_ str,
    interests: &'_ [S]
) -> Result<Self> where
    S: AsRef<str>, 
[src]

Create a new instance of EventSource and wait for a connection to be established.

pub fn reconnect(&mut self) -> bool[src]

Get whether the instance should reconnect.

Defaults to true.

pub fn set_reconnect(&mut self, reconnect: bool)[src]

Track whether the instance should reconnect.

pub fn register(&mut self, name: &str)[src]

Register interest in an event.

pub async fn recv<'_>(&'_ self) -> Result<MessageEvent>[src]

Receive a message from the stream.

pub fn ready_state(&self) -> ReadyState[src]

Access the EventSource's connection state.

Trait Implementations

impl AsRef<EventTarget> for EventSource[src]

impl Debug for EventSource[src]

impl Drop for EventSource[src]

impl PinnedDrop for EventSource[src]

impl Stream for EventSource[src]

type Item = Result<MessageEvent>

Values yielded by the stream.

impl<'pin> Unpin for EventSource where
    __EventSource<'pin>: Unpin
[src]

impl UnsafeUnpin for EventSource[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoStream for I where
    I: Stream
[src]

type Item = <I as Stream>::Item

The type of the elements being iterated over.

type IntoStream = I

Which kind of stream are we turning this into?

impl<T> StreamExt for T where
    T: Stream + ?Sized
[src]

impl<S> StreamExt for S where
    S: Stream + ?Sized

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future