pub struct WebSocketClient { /* private fields */ }Expand description
WebSocket client with automatic reconnection.
Handles connection state, callbacks, and rate limiting. See module docs for architecture details.
Implementations§
Source§impl WebSocketClient
impl WebSocketClient
Sourcepub async fn connect_stream(
config: WebSocketConfig,
keyed_quotas: Vec<(String, Quota)>,
default_quota: Option<Quota>,
post_reconnect: Option<Arc<dyn Fn() + Send + Sync>>,
) -> Result<(MessageReader, Self), Error>
pub async fn connect_stream( config: WebSocketConfig, keyed_quotas: Vec<(String, Quota)>, default_quota: Option<Quota>, post_reconnect: Option<Arc<dyn Fn() + Send + Sync>>, ) -> Result<(MessageReader, Self), Error>
Creates a websocket client in stream mode that returns a MessageReader.
Returns a stream that the caller owns and reads from directly. Automatic reconnection
is disabled because the reader cannot be replaced internally. On disconnection, the
client transitions to CLOSED state and the caller must manually reconnect by calling
connect_stream again.
Use stream mode when you need custom reconnection logic, direct control over message reading, or fine-grained backpressure handling.
See WebSocketConfig documentation for comparison with handler mode.
§Errors
Returns an error if the connection cannot be established.
Sourcepub async fn connect(
config: WebSocketConfig,
message_handler: Option<MessageHandler>,
ping_handler: Option<PingHandler>,
post_reconnection: Option<Arc<dyn Fn() + Send + Sync>>,
keyed_quotas: Vec<(String, Quota)>,
default_quota: Option<Quota>,
) -> Result<Self, Error>
pub async fn connect( config: WebSocketConfig, message_handler: Option<MessageHandler>, ping_handler: Option<PingHandler>, post_reconnection: Option<Arc<dyn Fn() + Send + Sync>>, keyed_quotas: Vec<(String, Quota)>, default_quota: Option<Quota>, ) -> Result<Self, Error>
Creates a websocket client in handler mode with automatic reconnection.
The handler is called for each incoming message on an internal task. Automatic reconnection is enabled with exponential backoff. On disconnection, the client automatically attempts to reconnect and replaces the internal reader (the handler continues working seamlessly).
Use handler mode for simplified connection management, automatic reconnection, Python bindings, or callback-based message handling.
See WebSocketConfig documentation for comparison with stream mode.
§Errors
Returns an error if:
- The connection cannot be established.
message_handlerisNone(useconnect_streaminstead).
Sourcepub fn connection_mode(&self) -> ConnectionMode
pub fn connection_mode(&self) -> ConnectionMode
Returns the current connection mode.
Sourcepub fn connection_mode_atomic(&self) -> Arc<AtomicU8> ⓘ
pub fn connection_mode_atomic(&self) -> Arc<AtomicU8> ⓘ
Returns a clone of the connection mode atomic for external state tracking.
This allows adapter clients to track connection state across reconnections without message-passing delays.
Sourcepub fn is_active(&self) -> bool
pub fn is_active(&self) -> bool
Check if the client connection is active.
Returns true if the client is connected and has not been signalled to disconnect.
The client will automatically retry connection based on its configuration.
Sourcepub fn is_disconnected(&self) -> bool
pub fn is_disconnected(&self) -> bool
Check if the client is disconnected.
Sourcepub fn is_reconnecting(&self) -> bool
pub fn is_reconnecting(&self) -> bool
Check if the client is reconnecting.
Returns true if the client lost connection and is attempting to reestablish it.
The client will automatically retry connection based on its configuration.
Sourcepub fn is_disconnecting(&self) -> bool
pub fn is_disconnecting(&self) -> bool
Check if the client is disconnecting.
Returns true if the client is in disconnect mode.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Check if the client is closed.
Returns true if the client has been explicitly disconnected or reached
maximum reconnection attempts. In this state, the client cannot be reused
and a new client must be created for further connections.
Sourcepub fn notify_closed(&self)
pub fn notify_closed(&self)
Signals that the caller’s reader has observed EOF or a fatal error.
In stream mode the controller has no visibility into the caller-owned reader.
Call this method when reader.next().await returns None or an unrecoverable
error so the controller transitions to Closed and dependent tasks shut down.
For peer-initiated close frames (Message::Close), use disconnect
instead so the writer can send the close reply before shutting down.
This is a no-op if the connection is already closed or disconnecting.
Sourcepub async fn disconnect(&self)
pub async fn disconnect(&self)
Set disconnect mode to true.
Controller task will periodically check the disconnect mode and shutdown the client if it is alive
Sourcepub async fn send_text(
&self,
data: String,
keys: Option<&[Ustr]>,
) -> Result<(), SendError>
pub async fn send_text( &self, data: String, keys: Option<&[Ustr]>, ) -> Result<(), SendError>
Sends the given text data to the server.
Returns Ok(()) when the message is enqueued to the writer channel. This does NOT
guarantee delivery: if a disconnect occurs concurrently, the writer task may drop the
message. During reconnection, messages are buffered and replayed on the new connection.
§Errors
Returns a websocket error if unable to send.
Sourcepub async fn send_bytes(
&self,
data: Vec<u8>,
keys: Option<&[Ustr]>,
) -> Result<(), SendError>
pub async fn send_bytes( &self, data: Vec<u8>, keys: Option<&[Ustr]>, ) -> Result<(), SendError>
Sends the given bytes data to the server.
Returns Ok(()) when the message is enqueued to the writer channel. This does NOT
guarantee delivery: if a disconnect occurs concurrently, the writer task may drop the
message. During reconnection, messages are buffered and replayed on the new connection.
§Errors
Returns a websocket error if unable to send.
Trait Implementations§
Source§impl Debug for WebSocketClient
impl Debug for WebSocketClient
Source§impl Drop for WebSocketClient
impl Drop for WebSocketClient
Source§impl<'py> IntoPyObject<'py> for WebSocketClient
impl<'py> IntoPyObject<'py> for WebSocketClient
Source§type Target = WebSocketClient
type Target = WebSocketClient
Source§type Output = Bound<'py, <WebSocketClient as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <WebSocketClient as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClass for WebSocketClient
impl PyClass for WebSocketClient
Source§impl PyClassImpl for WebSocketClient
impl PyClassImpl for WebSocketClient
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// WebSocket client with automatic reconnection.
///
/// Handles connection state, callbacks, and rate limiting.
/// See module docs for architecture details.
const RAW_DOC: &'static CStr = /// WebSocket client with automatic reconnection. /// /// Handles connection state, callbacks, and rate limiting. /// See module docs for architecture details.
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type Layout = <<WebSocketClient as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<WebSocketClient>
type Layout = <<WebSocketClient as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<WebSocketClient>
Source§type ThreadChecker = NoopThreadChecker
type ThreadChecker = NoopThreadChecker
type Inventory = Pyo3MethodsInventoryForWebSocketClient
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
Source§fn dict_offset() -> Option<PyObjectOffset>
fn dict_offset() -> Option<PyObjectOffset>
Source§fn weaklist_offset() -> Option<PyObjectOffset>
fn weaklist_offset() -> Option<PyObjectOffset>
Source§impl PyStubType for WebSocketClient
impl PyStubType for WebSocketClient
Source§fn type_output() -> TypeInfo
fn type_output() -> TypeInfo
Source§fn type_input() -> TypeInfo
fn type_input() -> TypeInfo
Source§impl PyTypeInfo for WebSocketClient
impl PyTypeInfo for WebSocketClient
Source§const NAME: &str = <Self as ::pyo3::PyClass>::NAME
const NAME: &str = <Self as ::pyo3::PyClass>::NAME
prefer using ::type_object(py).name() to get the correct runtime value
Source§const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
prefer using ::type_object(py).module() to get the correct runtime value
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
Source§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
impl DerefToPyAny for WebSocketClient
impl ExtractPyClassWithClone for WebSocketClient
Auto Trait Implementations§
impl Freeze for WebSocketClient
impl !RefUnwindSafe for WebSocketClient
impl Send for WebSocketClient
impl Sync for WebSocketClient
impl Unpin for WebSocketClient
impl UnsafeUnpin for WebSocketClient
impl !UnwindSafe for WebSocketClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
Source§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.Source§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
Source§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
Source§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
Use ::classinfo_object() instead and format the type name at runtime. Note that using built-in cast features is often better than manual PyTypeCheck usage.