pub struct MessageHandler { /* private fields */ }Expand description
Handles incoming WebSocket messages
Implementations§
Source§impl MessageHandler
impl MessageHandler
Sourcepub fn new(
orderbooks: Arc<RwLock<HashMap<String, LocalOrderbook>>>,
user_states: Arc<RwLock<HashMap<String, UserState>>>,
price_histories: Arc<RwLock<HashMap<PriceHistoryKey, PriceHistory>>>,
subscribed_user: Arc<RwLock<Option<String>>>,
) -> Self
pub fn new( orderbooks: Arc<RwLock<HashMap<String, LocalOrderbook>>>, user_states: Arc<RwLock<HashMap<String, UserState>>>, price_histories: Arc<RwLock<HashMap<PriceHistoryKey, PriceHistory>>>, subscribed_user: Arc<RwLock<Option<String>>>, ) -> Self
Create a new message handler with shared state
Sourcepub async fn handle_message(&self, text: &str) -> Vec<WsEvent>
pub async fn handle_message(&self, text: &str) -> Vec<WsEvent>
Handle an incoming message and return events
Sourcepub async fn init_orderbook(&self, orderbook_id: &str)
pub async fn init_orderbook(&self, orderbook_id: &str)
Initialize orderbook state for a subscription.
This must be called before subscribing to orderbook updates to ensure
the local state exists when the first snapshot arrives. If not called,
the handler will create the state on first message, but there may be
a brief window where get_orderbook() returns None.
Uses atomic entry API to avoid race conditions with message handlers. Thread-safe: multiple concurrent calls are safe due to interior write lock.
Sourcepub async fn init_user_state(&self, user: &str)
pub async fn init_user_state(&self, user: &str)
Initialize user state for a subscription.
This must be called before subscribing to user events to ensure state is ready when the first event arrives. Also sets the tracked user for this connection (single user per connection model).
Thread-safe: uses interior write locks for both the subscribed user tracking and the user state map. Multiple concurrent calls are safe but will serialize on lock acquisition. State mutations from incoming events are applied atomically via the same lock.
Sourcepub async fn clear_subscribed_user(&self, user: &str)
pub async fn clear_subscribed_user(&self, user: &str)
Clear the subscribed user
Sourcepub async fn init_price_history(
&self,
orderbook_id: &str,
resolution: &str,
include_ohlcv: bool,
)
pub async fn init_price_history( &self, orderbook_id: &str, resolution: &str, include_ohlcv: bool, )
Initialize price history state for a subscription.
Creates an empty price history container for the given orderbook and resolution. The actual price data will be populated when the snapshot message arrives from the server.
§Arguments
orderbook_id- The orderbook identifier (e.g., “BTC-USD:main”)resolution- The candle resolution (e.g., “1m”, “5m”, “1h”, “1d”)include_ohlcv- Whether OHLCV candle data should be tracked
§Example
handler.init_price_history("BTC-USD:main", "1m", true).await;Sourcepub async fn clear_orderbook(&self, orderbook_id: &str)
pub async fn clear_orderbook(&self, orderbook_id: &str)
Clear orderbook state
Sourcepub async fn clear_user_state(&self, user: &str)
pub async fn clear_user_state(&self, user: &str)
Clear user state
Sourcepub async fn clear_price_history(&self, orderbook_id: &str, resolution: &str)
pub async fn clear_price_history(&self, orderbook_id: &str, resolution: &str)
Clear price history state
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MessageHandler
impl !RefUnwindSafe for MessageHandler
impl Send for MessageHandler
impl Sync for MessageHandler
impl Unpin for MessageHandler
impl !UnwindSafe for MessageHandler
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more