pub struct WsSession { /* private fields */ }Expand description
A WebSocket session of the data channel /ws.
It connects in the background, keeps the connection alive with ping frames, and when the connection drops it
reconnects with growing pauses and sends the subscriptions again (each with a fresh access token). Frames
arrive parsed on the channel given to WsSession::spawn. Dropping the session closes it.
§Examples
use brokapi_alor_protocol::http::client::AlorHttpClient;
use brokapi_alor_protocol::ws::models::{WsIncoming, WsSubscribe};
use brokapi_alor_protocol::ws::session::WsSession;
let http = AlorHttpClient::new(cfg)?;
let (tx, rx) = kanal::bounded_async(64);
let session = WsSession::spawn(http, tx);
session.subscribe(WsSubscribe::quotes("q1", "MOEX", "SBER", Some("TQBR"))).await?;
loop {
if let WsIncoming::Data { guid, .. } = rx.recv().await? {
assert_eq!(guid, "q1");
break;
}
}Implementations§
Source§impl WsSession
impl WsSession
Sourcepub fn spawn(http: AlorHttpClient, event_tx: AsyncSender<WsIncoming>) -> Self
pub fn spawn(http: AlorHttpClient, event_tx: AsyncSender<WsIncoming>) -> Self
Starts a session with the default options. It returns at once and connects in the background.
Sourcepub fn spawn_with(
http: AlorHttpClient,
event_tx: AsyncSender<WsIncoming>,
options: WsOptions,
) -> Self
pub fn spawn_with( http: AlorHttpClient, event_tx: AsyncSender<WsIncoming>, options: WsOptions, ) -> Self
Starts a session with the given options.
Sourcepub fn dropped_events(&self) -> u64
pub fn dropped_events(&self) -> u64
How many parsed frames were dropped because the receiving channel was full.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
true while the connection is up and the subscriptions are sent.
Sourcepub async fn subscribe(&self, sub: WsSubscribe) -> Result<(), AlorApiError>
pub async fn subscribe(&self, sub: WsSubscribe) -> Result<(), AlorApiError>
Subscribes. The request is remembered and sent again after every reconnect until WsSession::unsubscribe.
§Errors
AlorApiError::Subscription at 5000 subscriptions or when the session is closed.
Sourcepub async fn unsubscribe(&self, guid: &str) -> Result<(), AlorApiError>
pub async fn unsubscribe(&self, guid: &str) -> Result<(), AlorApiError>
Cancels the subscription named by guid: forgets it and sends unsubscribe.
§Errors
AlorApiError::Subscription when the session is closed.