Skip to main content

bybit/ws/
messages.rs

1use crate::{
2    Topic,
3    ws::{IncomingMessage, OutgoingMessage},
4};
5
6/// Commands from a [`Handle`](crate::ws::Handle) to the driver.
7#[derive(Debug)]
8pub enum Command {
9    /// Open the connection.
10    Connect,
11    /// Send a raw message. Subscriptions sent this way are not restored
12    /// after a reconnect; use [`Command::Subscribe`] for that.
13    Send(OutgoingMessage),
14    /// Subscribe now (if connected) and after every reconnect.
15    Subscribe(Vec<Topic>),
16    /// Unsubscribe and stop restoring the topics.
17    Unsubscribe(Vec<Topic>),
18    /// Close the connection.
19    Disconnect,
20}
21
22/// Events from the driver, received from [`Stream::new`](crate::ws::Stream::new).
23#[derive(Debug)]
24pub enum Event {
25    /// The connection is open. With credentials configured, `auth` has been
26    /// sent; subscriptions follow once it succeeds.
27    Connected,
28    /// The stream accepted the credentials (private streams).
29    Authenticated,
30    /// The stream rejected the credentials; no topics are subscribed on this
31    /// connection.
32    AuthFailed {
33        /// `ret_msg` of the rejected `auth`.
34        ret_msg: Option<String>,
35    },
36    /// The stream rejected a `subscribe` sent by the driver for `topics`.
37    SubscribeFailed {
38        /// Topics of the rejected `subscribe`.
39        topics: Vec<Topic>,
40        /// `ret_msg` of the rejected `subscribe`.
41        ret_msg: Option<String>,
42    },
43    /// A message from the stream (data or a reply to a command).
44    Message(IncomingMessage),
45    /// A WebSocket text frame arrived but could not be deserialized.
46    /// The connection stays open — the raw error description is included.
47    ParseError(String),
48    /// `dropped` data events (`Message`/`ParseError`) were discarded because
49    /// the event queue was full. Local state built from the stream (e.g. an
50    /// order book) may be stale and should be resynchronized.
51    Lagged {
52        /// Number of dropped events.
53        dropped: u64,
54    },
55    /// The connection was lost; the driver reconnects after `delay_ms`.
56    Reconnecting {
57        /// Number of this reconnect attempt, starting at 1.
58        attempt: u32,
59        /// Delay before the attempt, milliseconds.
60        delay_ms: u64,
61    },
62    /// The connection is closed and will not be reopened automatically.
63    Disconnected {
64        /// Why the connection was closed.
65        reason: DisconnectReason,
66    },
67}
68
69/// Why the connection was closed.
70#[derive(Debug, Clone)]
71pub enum DisconnectReason {
72    /// [`Handle::disconnect`](crate::ws::Handle::disconnect) was called (or all handles were dropped).
73    Requested,
74    /// The server closed the connection and reconnecting gave up.
75    RemoteClosed,
76    /// No heartbeat reply in time and reconnecting gave up.
77    PongTimeout,
78    /// A connection or protocol error and reconnecting gave up.
79    Error(String),
80}