pub struct O2WebSocket {
url: String,
config: WsConfig,
inner: Arc<Mutex<WsInner>>,
connected: Arc<AtomicBool>,
should_run: Arc<AtomicBool>,
last_pong: Arc<Mutex<Instant>>,
lifecycle_tx: Arc<Sender<WsLifecycleEvent>>,
reader_handle: Option<JoinHandle<()>>,
ping_handle: Option<JoinHandle<()>>,
}Expand description
WebSocket client for O2 Exchange real-time data.
Supports auto-reconnect, subscription tracking, heartbeat, and per-subscription channels for safe concurrent access.
Fields§
§url: String§config: WsConfig§inner: Arc<Mutex<WsInner>>§connected: Arc<AtomicBool>§should_run: Arc<AtomicBool>§last_pong: Arc<Mutex<Instant>>§lifecycle_tx: Arc<Sender<WsLifecycleEvent>>§reader_handle: Option<JoinHandle<()>>§ping_handle: Option<JoinHandle<()>>Implementations§
Source§impl O2WebSocket
impl O2WebSocket
Sourcepub async fn connect_with_config(
url: &str,
config: WsConfig,
) -> Result<Self, O2Error>
pub async fn connect_with_config( url: &str, config: WsConfig, ) -> Result<Self, O2Error>
Connect with custom configuration.
async fn do_connect(&mut self) -> Result<(), O2Error>
async fn read_loop( stream: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>, inner: Arc<Mutex<WsInner>>, connected: Arc<AtomicBool>, should_run: Arc<AtomicBool>, last_pong: Arc<Mutex<Instant>>, )
async fn ping_loop( inner: Arc<Mutex<WsInner>>, connected: Arc<AtomicBool>, should_run: Arc<AtomicBool>, last_pong: Arc<Mutex<Instant>>, ping_interval: Duration, pong_timeout: Duration, )
async fn reconnect_loop( url: &str, config: &WsConfig, inner: Arc<Mutex<WsInner>>, connected: Arc<AtomicBool>, should_run: Arc<AtomicBool>, last_pong: Arc<Mutex<Instant>>, lifecycle_tx: Arc<Sender<WsLifecycleEvent>>, )
Sourcepub fn subscribe_lifecycle(&self) -> Receiver<WsLifecycleEvent>
pub fn subscribe_lifecycle(&self) -> Receiver<WsLifecycleEvent>
Subscribe to lifecycle/reconnect events.
async fn send_json(&self, value: Value) -> Result<(), O2Error>
fn add_subscription(inner: &mut WsInner, sub: Value)
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if the WebSocket is currently connected.
Sourcepub async fn stream_depth(
&self,
market_id: &str,
precision: &DepthPrecision,
) -> Result<TypedStream<DepthUpdate>, O2Error>
pub async fn stream_depth( &self, market_id: &str, precision: &DepthPrecision, ) -> Result<TypedStream<DepthUpdate>, O2Error>
Subscribe to order book depth. Returns a stream of Result<DepthUpdate, O2Error>.
precision is a level index as a string (e.g. "1" for finest).
Valid range: "1"–"18". The SDK sends 10^precision on the wire,
matching the internal backend convention. All levels support live
delta streaming.
Note: Prefer [O2Client::stream_depth] which validates precision
and resolves market IDs by name.
Sourcepub async fn stream_orders(
&self,
identities: &[Identity],
) -> Result<TypedStream<OrderUpdate>, O2Error>
pub async fn stream_orders( &self, identities: &[Identity], ) -> Result<TypedStream<OrderUpdate>, O2Error>
Subscribe to order updates. Returns a stream of Result<OrderUpdate, O2Error>.
Sourcepub async fn stream_trades(
&self,
market_id: &str,
) -> Result<TypedStream<TradeUpdate>, O2Error>
pub async fn stream_trades( &self, market_id: &str, ) -> Result<TypedStream<TradeUpdate>, O2Error>
Subscribe to trades. Returns a stream of Result<TradeUpdate, O2Error>.
Sourcepub async fn stream_balances(
&self,
identities: &[Identity],
) -> Result<TypedStream<BalanceUpdate>, O2Error>
pub async fn stream_balances( &self, identities: &[Identity], ) -> Result<TypedStream<BalanceUpdate>, O2Error>
Subscribe to balance updates. Returns a stream of Result<BalanceUpdate, O2Error>.
Sourcepub async fn stream_nonce(
&self,
identities: &[Identity],
) -> Result<TypedStream<NonceUpdate>, O2Error>
pub async fn stream_nonce( &self, identities: &[Identity], ) -> Result<TypedStream<NonceUpdate>, O2Error>
Subscribe to nonce updates. Returns a stream of Result<NonceUpdate, O2Error>.
Sourcepub async fn unsubscribe_depth(&self, market_id: &str) -> Result<(), O2Error>
pub async fn unsubscribe_depth(&self, market_id: &str) -> Result<(), O2Error>
Unsubscribe from depth updates.
Sourcepub async fn unsubscribe_orders(&self) -> Result<(), O2Error>
pub async fn unsubscribe_orders(&self) -> Result<(), O2Error>
Unsubscribe from order updates.
Sourcepub async fn unsubscribe_trades(&self, market_id: &str) -> Result<(), O2Error>
pub async fn unsubscribe_trades(&self, market_id: &str) -> Result<(), O2Error>
Unsubscribe from trade updates.
Sourcepub async fn unsubscribe_balances(
&self,
identities: &[Identity],
) -> Result<(), O2Error>
pub async fn unsubscribe_balances( &self, identities: &[Identity], ) -> Result<(), O2Error>
Unsubscribe from balance updates.
Sourcepub async fn unsubscribe_nonce(
&self,
identities: &[Identity],
) -> Result<(), O2Error>
pub async fn unsubscribe_nonce( &self, identities: &[Identity], ) -> Result<(), O2Error>
Unsubscribe from nonce updates.
Sourcepub fn is_terminated(&self) -> bool
pub fn is_terminated(&self) -> bool
Check if the WebSocket has been permanently terminated (max reconnect attempts exhausted or explicitly stopped).
Sourcepub async fn disconnect(&self) -> Result<(), O2Error>
pub async fn disconnect(&self) -> Result<(), O2Error>
Close the WebSocket connection and stop all tasks.