Skip to main content

WebSocketClient

Struct WebSocketClient 

Source
pub struct WebSocketClient { /* private fields */ }
Expand description

Synchronous WebSocket client.

All operations block the caller. No tokio runtime required. Internally owns one OS thread per active connection (the supervisor/owner thread).

Implementations§

Source§

impl WebSocketClient

Source

pub fn new(config: ConnectionConfig) -> Self

Create a new WebSocket client with default reconnection + health check config.

Source

pub fn with_reconnection_config( config: ConnectionConfig, reconnection_config: ReconnectionConfig, ) -> Self

Create a new WebSocket client with custom reconnection config.

Source

pub fn with_health_check_config( config: ConnectionConfig, health_check_config: HealthCheckConfig, ) -> Self

Create a new WebSocket client with custom health check config.

Source

pub fn with_full_config( config: ConnectionConfig, reconnection_config: ReconnectionConfig, health_check_config: HealthCheckConfig, ) -> Self

Create a new WebSocket client with full custom config.

Source

pub fn state(&self) -> ConnectionState

Current connection state (snapshot).

Source

pub fn is_closed(&self) -> bool

Returns true once the client has been disconnected and cannot be reused.

Source

pub fn is_connected(&self) -> bool

True iff the supervisor reports a Connected state.

Source

pub fn events(&self) -> &Arc<Mutex<Receiver<ConnectionEvent>>>

Reference to the event receiver. Lifecycle events arrive here (bounded channel, drop-newest on saturation).

Source

pub fn state_events(&self) -> &Arc<Mutex<Receiver<ConnectionEvent>>>

Semantic alias for events.

Source

pub fn messages(&self) -> Arc<MessageReceiver>

Get the blocking inbound-message receiver. Idempotent; subsequent calls return the same Arc<MessageReceiver>.

Source

pub fn connect(&self) -> Result<(), MarketDataError>

Connect to the WebSocket server and authenticate. Blocks until either authentication succeeds or fails.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn disconnect(&self) -> Result<(), MarketDataError>

Disconnect gracefully with the default drain timeout (DEFAULT_SHUTDOWN_TIMEOUT, 5 seconds).

See shutdown_with_timeout for detailed sequencing — this is a thin wrapper.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn shutdown_with_timeout( &self, timeout_dur: Duration, ) -> Result<(), MarketDataError>

Disconnect with a caller-supplied drain timeout.

Sequence:

  1. Set the supervisor’s should_stop flag.
  2. Drop the writer-side sender so the owner loop’s drain step cannot pick up new frames.
  3. Wait up to timeout_dur for the supervisor thread to signal exit (the owner loop polls should_stop every READ_POLL_INTERVAL, drains its write queue, sends Close, then waits up to ~2 s for the peer’s Close ack).
  4. On signal: best-effort join the thread.
  5. On timeout: leave the thread to wind down on its own (it has already been instructed to stop and will not resurrect the connection); detach the handle.
  6. Emit Disconnected { intent: Client } and update state.

timeout_dur of zero is valid and behaves as “fire-and-forget”: the call returns immediately, the supervisor exits in the background.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn force_close(&self) -> Result<(), MarketDataError>

Force-close without waiting for the supervisor.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn subscribe(&self, sub: StockSubscription) -> Result<(), MarketDataError>

Subscribe to a stock-domain stream.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn subscribe_futopt( &self, sub: FutOptSubscription, ) -> Result<(), MarketDataError>

Subscribe to a FutOpt-domain stream.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn unsubscribe( &self, ids: impl IntoIterator<Item = impl Into<String>>, ) -> Result<(), MarketDataError>

Unsubscribe by server id or local key.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn subscriptions(&self) -> Vec<SubscribeRequest>

Get all active subscriptions.

Source

pub fn subscription_keys(&self) -> Vec<String>

Get list of active subscription keys.

Source

pub fn subscription_count(&self) -> usize

Number of currently active subscriptions.

Source

pub fn messages_dropped_total(&self) -> u64

Total number of inbound messages dropped due to consumer-side channel saturation since this client was constructed.

Drop-newest backpressure: when the message buffer is full, new arrivals are discarded rather than blocking the read thread. A non-zero value usually indicates the consumer (messages() reader) is too slow or stalled.

Source

pub fn events_dropped_total(&self) -> u64

Total number of lifecycle ConnectionEvents dropped due to event- channel saturation since this client was constructed.

Mirrors Self::messages_dropped_total for the lifecycle event channel. Drop-newest backpressure: when the event channel is full, new events are discarded rather than blocking the supervisor.

Counter is monotonic and thread-safe (AtomicU64).

Source

pub fn is_subscribed(&self, channel: &Channel, symbol: &str) -> bool

Returns true iff at least one active subscription matches the given channel and symbol. Modifier-suffixed forms (:afterhours, :oddlot) are matched alongside the base form.

Source

pub fn reconnect(&self) -> Result<(), MarketDataError>

Manually reconnect. Calls disconnect() then connect() — simpler and safer than poking the supervisor.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Source

pub fn send(&self, request: WebSocketRequest) -> Result<(), MarketDataError>

Send an arbitrary WebSocket request frame.

§Errors

Returns MarketDataError on transport, protocol, deserialization, validation, or peer-initiated failures.

Trait Implementations§

Source§

impl Drop for WebSocketClient

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more