Binance C++ Connector 2.0.0
Official C++ connectors for the Binance APIs
Loading...
Searching...
No Matches
binance::common::WebsocketApi Class Reference

The WebSocket API transport (request / response) shared by every product. More...

#include <websocket_api.hpp>

Public Types

using ConnectSignature = void(std::exception_ptr)
using SendSignature = void(std::exception_ptr, std::vector<WebsocketApiRawResponse>)
using SubscribeSignature = void(std::exception_ptr, WebsocketApiRawSubscription)

Public Member Functions

 WebsocketApi (WebsocketApiConfig configuration, detail::ClientOptions options={})
 Throws ConfigError if url or proxy is invalid, or ConnectorClientError if the private key cannot be loaded.
 ~WebsocketApi ()
 WebsocketApi (const WebsocketApi &)=delete
WebsocketApi & operator= (const WebsocketApi &)=delete
 WebsocketApi (WebsocketApi &&)=delete
WebsocketApi & operator= (WebsocketApi &&)=delete
const WebsocketApiConfig & configuration () const noexcept
 The configuration, as given but without private_key_passphrase (the key is loaded).
const SignatureGenerator & signature_generator () const noexcept
boost::asio::any_io_executor get_executor () const noexcept
template<class CompletionToken>
auto async_connect (CompletionToken &&token)
 Opens every connection of the pool that is not open, each within connect_timeout.
void connect ()
template<class CompletionToken>
auto async_disconnect (CompletionToken &&token)
 Stops reconnecting, waits up to 30 s for pending responses, then closes every connection with code 1000.
void disconnect ()
bool is_connected () const
 True if at least one connection is open.
void ping_server ()
 Sends a ping frame on every open connection.
template<class F>
Subscription on_event (F &&callback, bool with_messages=true)
 Registers a lifecycle callback (WebsocketEvent).
template<class CompletionToken>
auto async_send_message (std::string method, Params payload, WebsocketMessageSendOptions options, CompletionToken &&token)
 Sends a request and completes with its response: one per connection for is_session_logon / is_session_logout (sent on every connection), else exactly one.
std::vector< WebsocketApiRawResponse > send_message (std::string method, Params payload, WebsocketMessageSendOptions options={})
 Blocking async_send_message.
template<class CompletionToken>
auto async_subscribe (std::string method, Params payload, WebsocketMessageSendOptions options, CompletionToken &&token)
 Sends a request that opens a user-data subscription (e.g.
WebsocketApiRawSubscription subscribe (std::string method, Params payload, WebsocketMessageSendOptions options={})
 Blocking async_subscribe.
WebsocketStreamHandle create_stream (std::string id={})
 A handle receiving every user-data event from any connection, for subscriptions managed by the caller.

Detailed Description

The WebSocket API transport (request / response) shared by every product.

  • A pool of mode.pool_size() connections, used round-robin.
  • Requests are matched to responses by id and time out after timeout.
  • Dropped connections reconnect after reconnect_delay, unless the close code says it is permanent (see WebsocketConnectionFailureReason).
  • Connections are renewed every 23 hours, and when the server announces serverShutdown, without losing in-flight requests.
  • With auto_session_relogon, a session logged on with session.logon is logged on again after every reconnect.
  • User-data subscriptions made with subscribe are sent again after every reconnect and renewal (after the session logon, if any), on the connection that made them. Their stream ends them (see async_subscribe).
  • Other requests about subscriptions (userDataStream.unsubscribe, session.subscriptions) sent with send_message go to one connection, like any request, and Binance numbers the subscriptions of each connection from 0: with pool_size() > 1, end a subscription through its stream. A successful userDataStream.unsubscribe also stops sending the subscriptions it ended again.

The client runs on its own I/O thread, or on configuration.executor when set. Completion handlers run on their associated executor, or on the client's (get_executor()). Callbacks (events, user data) run on the client's executor unless bound to another, so they should not block. Every member function is thread-safe. Blocking calls (connect, disconnect, send_message, subscribe) throw WebsocketError if made from the client's executor or a thread running it (for io_context and thread_pool executors and their strands); with other executors, do not make them from its threads. The other calls never block on the executor.

Errors are WebsocketError subclasses, or ConnectorClientError for a request that cannot be built (missing api_key, signing failure).

Destroying the client closes its connections at once and fails pending requests with WebsocketCancelledError; call disconnect() first for a graceful close.

Member Typedef Documentation

◆ ConnectSignature

using binance::common::WebsocketApi::ConnectSignature = void(std::exception_ptr)

◆ SendSignature

using binance::common::WebsocketApi::SendSignature = void(std::exception_ptr, std::vector<WebsocketApiRawResponse>)

◆ SubscribeSignature

Constructor & Destructor Documentation

◆ WebsocketApi() [1/3]

binance::common::WebsocketApi::WebsocketApi ( WebsocketApiConfig configuration,
detail::ClientOptions options = {} )
explicit

Throws ConfigError if url or proxy is invalid, or ConnectorClientError if the private key cannot be loaded.

◆ ~WebsocketApi()

binance::common::WebsocketApi::~WebsocketApi ( )

◆ WebsocketApi() [2/3]

binance::common::WebsocketApi::WebsocketApi ( const WebsocketApi & )
delete

◆ WebsocketApi() [3/3]

binance::common::WebsocketApi::WebsocketApi ( WebsocketApi && )
delete

Member Function Documentation

◆ operator=() [1/2]

WebsocketApi & binance::common::WebsocketApi::operator= ( const WebsocketApi & )
delete

◆ operator=() [2/2]

WebsocketApi & binance::common::WebsocketApi::operator= ( WebsocketApi && )
delete

◆ configuration()

const WebsocketApiConfig & binance::common::WebsocketApi::configuration ( ) const
nodiscardnoexcept

The configuration, as given but without private_key_passphrase (the key is loaded).

◆ signature_generator()

const SignatureGenerator & binance::common::WebsocketApi::signature_generator ( ) const
nodiscardnoexcept

◆ get_executor()

boost::asio::any_io_executor binance::common::WebsocketApi::get_executor ( ) const
nodiscardnoexcept

◆ async_connect()

template<class CompletionToken>
auto binance::common::WebsocketApi::async_connect ( CompletionToken && token)
inline

Opens every connection of the pool that is not open, each within connect_timeout.

Completes once all are open, or with the first failure. A call made while connecting completes with that attempt; one made while disconnecting connects once that is done.

◆ connect()

void binance::common::WebsocketApi::connect ( )

◆ async_disconnect()

template<class CompletionToken>
auto binance::common::WebsocketApi::async_disconnect ( CompletionToken && token)
inline

Stops reconnecting, waits up to 30 s for pending responses, then closes every connection with code 1000.

The session logon is forgotten.

◆ disconnect()

void binance::common::WebsocketApi::disconnect ( )

◆ is_connected()

bool binance::common::WebsocketApi::is_connected ( ) const
nodiscard

True if at least one connection is open.

◆ ping_server()

void binance::common::WebsocketApi::ping_server ( )

Sends a ping frame on every open connection.

◆ on_event()

template<class F>
Subscription binance::common::WebsocketApi::on_event ( F && callback,
bool with_messages = true )
inline

Registers a lifecycle callback (WebsocketEvent).

It runs on its associated executor if it has one, else on the client's executor. Message events are built and delivered only if some callback has with_messages.

◆ async_send_message()

template<class CompletionToken>
auto binance::common::WebsocketApi::async_send_message ( std::string method,
Params payload,
WebsocketMessageSendOptions options,
CompletionToken && token )
inline

Sends a request and completes with its response: one per connection for is_session_logon / is_session_logout (sent on every connection), else exactly one.

Supports per-operation cancellation (bind_cancellation_slot, cancel_after(timer, …)): it then completes with WebsocketCancelledError. A response that arrives later is dropped, but the request may still have been executed.

◆ send_message()

std::vector< WebsocketApiRawResponse > binance::common::WebsocketApi::send_message ( std::string method,
Params payload,
WebsocketMessageSendOptions options = {} )

Blocking async_send_message.

◆ async_subscribe()

template<class CompletionToken>
auto binance::common::WebsocketApi::async_subscribe ( std::string method,
Params payload,
WebsocketMessageSendOptions options,
CompletionToken && token )
inline

Sends a request that opens a user-data subscription (e.g.

userDataStream.subscribe) and completes with its response and a stream of its events ({"event": {...}} messages from that subscription). The client sends the request again whenever the connection reopens, and the server may then assign another subscriptionId. stream.unsubscribe(), or destroying the stream, ends the subscription: it sends userDataStream.unsubscribe with the current subscriptionId on the connection holding it, and stops sending the request again. Failures are reported as Error events. stream.stream() is the first subscriptionId. Per-operation cancellation is not supported: it completes with the response.

◆ subscribe()

WebsocketApiRawSubscription binance::common::WebsocketApi::subscribe ( std::string method,
Params payload,
WebsocketMessageSendOptions options = {} )

Blocking async_subscribe.

◆ create_stream()

WebsocketStreamHandle binance::common::WebsocketApi::create_stream ( std::string id = {})
nodiscard

A handle receiving every user-data event from any connection, for subscriptions managed by the caller.

id names it; it defaults to a random one. While a connection is renewed, both of its sockets deliver, so an event may arrive twice. Prefer subscribe.