WsClient::connect
Open a WebSocket connection and return a Session that can host multiple concurrent subscriptions.
Signature
pub async fn connect(&self) -> Result<Session, anyhow::Error>;Example
use {
anyhow::Result,
dango_sdk::{Session, WsClient},
};
#[tokio::main]
async fn main() -> Result<()> {
let session: Session = WsClient::new("wss://api-mainnet.dango.zone/graphql")?
.connect()
.await?;
let _ = session;
Ok(())
}Returns
Session — a multiplexed session over the new connection. Cloning is cheap. The connection closes when the last Session clone and every derived stream have been dropped.
Notes
- Performs the
connection_init/connection_ackhandshake before returning. - Spawns a background
tokiotask that drives the socket (handles subscribe commands, pings, server messages). Requires a multi-threaded runtime —tokio::spawnis called internally. - Errors:
WebSocket connection failed: …— TLS handshake or TCP error.failed to send connection_init: …— initial write failed.unexpected message before connection_ack: …— server replied off-protocol.WebSocket closed before connection_ack— server hung up during handshake.
See also
Session::subscribe— open a subscription on the session.subscribe— one-shot dedicated connection.- Concepts: Subscriptions — multiplexed vs dedicated.