pub struct WebsocketEventEmitter { /* private fields */ }Implementations§
Source§impl WebsocketEventEmitter
impl WebsocketEventEmitter
pub fn new() -> Self
Sourcepub fn subscribe<F>(&self, callback: F) -> Subscription
pub fn subscribe<F>(&self, callback: F) -> Subscription
Subscribes to WebSocket events and returns a Subscription that allows event processing.
This method creates an unbounded channel for receiving WebSocket events and spawns an asynchronous task to process these events using the provided callback function.
§Arguments
callback- A mutable function that will be called for each received WebSocket event. The callback must be thread-safe and have a static lifetime.
§Returns
A Subscription that can be used to unsubscribe and stop event processing.
§Examples
let emitter = WebsocketEventEmitter::new();
let subscription = emitter.subscribe(|event| {
// Handle WebSocket event
println!(“Received event: {:?}”, event);
});
// Later, when no longer needed
subscription.unsubscribe();
Sourcepub fn emit(&self, event: &WebsocketEvent)
pub fn emit(&self, event: &WebsocketEvent)
Emits a WebSocket event to all registered subscribers.
This method sends the given event to all active subscribers. If a subscriber has been dropped without unsubscribing, a warning is logged and the subscriber is removed from the list.
§Arguments
event- The WebSocket event to be emitted to all subscribers.