Struct EventSource

Source
pub struct EventSource { /* private fields */ }
Expand description

Interface to interact with event-streams

Implementations§

Source§

impl EventSource

Source

pub fn new(url: &str) -> Result<EventSource, ParseError>

Create object and starts connection with event-stream

§Example
let event_source = EventSource::new("http://example.com/sub").unwrap();
Source

pub fn close(&self)

Close connection.

§Example
let event_source = EventSource::new("http://example.com/sub").unwrap();
event_source.close();
Source

pub fn on_open<F>(&self, listener: F)
where F: Fn() + Send + 'static,

Triggered when connection with stream is stabilished.

§Example
let event_source = EventSource::new("http://example.com/sub").unwrap();

event_source.on_open(|| {
    println!("Connection stabilished!");
});
Source

pub fn on_message<F>(&self, listener: F)
where F: Fn(Event) + Send + 'static,

Triggered when message event is received. Any event that doesn’t contain an event field is considered a message event.

§Example
let event_source = EventSource::new("http://example.com/sub").unwrap();

event_source.on_message(|message| {
    println!("Message received: {}", message.data);
});
Source

pub fn add_event_listener<F>(&self, event_type: &str, listener: F)
where F: Fn(Event) + Send + 'static,

Triggered when event with specified type is received.

Any connection error is notified as event with type error.

Events with no type defined have message as default type.

§Example
let event_source = EventSource::new("http://example.com/sub").unwrap();


event_source.add_event_listener("myEvent", |event| {
    println!("Event {} received: {}", event.type_, event.data);
});

event_source.add_event_listener("error", |error| {
    println!("Error: {}", error.data);
});

// equivalent to `on_message`
event_source.add_event_listener("message", |message| {
    println!("Message received: {}", message.data);
});
Source

pub fn state(&self) -> State

Returns client State.

§Example
use sse_client::State;
let event_source = EventSource::new("http://example.com/sub").unwrap();

assert_eq!(event_source.state(), State::Connecting);
Source

pub fn receiver(&self) -> Receiver<Event>

Returns a receiver that is triggered on any new message or error.

§Example
let event_source = EventSource::new("http://example.com/sub").unwrap();

for event in event_source.receiver().iter() {
    println!("New Message: {}", event.data);
}
let event_source = EventSource::new("http://example.com/sub").unwrap();
let rx = event_source.receiver();

let event = rx.recv().unwrap();
//...

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.