pub struct EventSource { /* private fields */ }
Expand description
Interface to interact with event-streams
Implementations§
Source§impl EventSource
impl EventSource
Sourcepub fn new(url: &str) -> Result<EventSource, ParseError>
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();
Sourcepub fn close(&self)
pub fn close(&self)
Close connection.
§Example
let event_source = EventSource::new("http://example.com/sub").unwrap();
event_source.close();
Sourcepub fn on_open<F>(&self, listener: F)
pub fn on_open<F>(&self, listener: F)
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!");
});
Sourcepub fn on_message<F>(&self, listener: F)
pub fn on_message<F>(&self, listener: F)
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);
});
Sourcepub fn add_event_listener<F>(&self, event_type: &str, listener: F)
pub fn add_event_listener<F>(&self, event_type: &str, listener: F)
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);
});
Sourcepub fn receiver(&self) -> Receiver<Event>
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§
impl Freeze for EventSource
impl RefUnwindSafe for EventSource
impl Send for EventSource
impl Sync for EventSource
impl Unpin for EventSource
impl UnwindSafe for EventSource
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more