[−][src]Struct sse_client::EventSource
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]
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!"); });
pub fn on_message<F>(&self, listener: F) where
F: Fn(Event) + Send + 'static,
[src]
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); });
pub fn add_event_listener<F>(&self, event_type: &str, listener: F) where
F: Fn(Event) + Send + 'static,
[src]
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); });
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
impl RefUnwindSafe for EventSource
impl Send for EventSource
impl Sync for EventSource
impl Unpin for EventSource
impl UnwindSafe for EventSource
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,