[][src]Struct sse_client::EventSource

pub struct EventSource { /* fields omitted */ }

Interface to interact with event-streams

Methods

impl EventSource[src]

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

Create object and starts connection with event-stream

Example

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

pub fn close(&self)[src]

Close connection.

Example

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

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

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!");
});

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

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);
});

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

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);
});

pub fn state(&self) -> State[src]

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);

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

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

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<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.