pub struct EventSource { /* fields omitted */ }Interface to interact with event-streams
Create object and starts connection with event-stream
let event_source = EventSource::new("http://example.com/sub").unwrap();
Close connection.
let event_source = EventSource::new("http://example.com/sub").unwrap();
event_source.close();
Triggered when connection with stream is stabilished.
let event_source = EventSource::new("http://example.com/sub").unwrap();
event_source.on_open(|| {
println!("Connection stabilished!");
});
Triggered when message event is received.
Any event that doesn't contain an event field is considered a message event.
let event_source = EventSource::new("http://example.com/sub").unwrap();
event_source.on_message(|message| {
println!("Message received: {}", message.data);
});
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.
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);
});
event_source.add_event_listener("message", |message| {
println!("Message received: {}", message.data);
});
Returns client State.
use sse_client::State;
let event_source = EventSource::new("http://example.com/sub").unwrap();
assert_eq!(event_source.state(), State::Connecting);