pub struct WebSocketClientInner { /* private fields */ }Expand description
WebSocketClient connects to a websocket server to read and send messages.
The client is opinionated about how messages are read and written. It assumes that data can only have one reader but multiple writers.
The client splits the connection into read and write halves. It moves the read half into a tokio task which keeps receiving messages from the server and calls a handler - a Python function that takes the data as its parameter. It stores the write half in the struct wrapped with an Arc Mutex. This way the client struct can be used to write data to the server from multiple scopes/tasks.
The client also maintains a heartbeat if given a duration in seconds. It’s preferable to set the duration slightly lower - heartbeat more frequently - than the required amount.
Implementations§
Source§impl WebSocketClientInner
impl WebSocketClientInner
Sourcepub async fn new_with_writer(
config: WebSocketConfig,
writer: SplitSink<BoxedWsTransport, Message>,
) -> Result<Self, TransportError>
pub async fn new_with_writer( config: WebSocketConfig, writer: SplitSink<BoxedWsTransport, Message>, ) -> Result<Self, TransportError>
Create an inner websocket client with an existing writer.
This is used for stream mode where the reader is owned by the caller.
§Errors
Returns an error if the exponential backoff configuration is invalid.
Sourcepub async fn connect_url(
config: WebSocketConfig,
message_handler: Option<MessageHandler>,
ping_handler: Option<PingHandler>,
) -> Result<Self, TransportError>
pub async fn connect_url( config: WebSocketConfig, message_handler: Option<MessageHandler>, ping_handler: Option<PingHandler>, ) -> Result<Self, TransportError>
Create an inner websocket client.
§Errors
Returns an error if:
- The connection to the server fails.
- The exponential backoff configuration is invalid.
Sourcepub async fn connect_with_server(
url: &str,
headers: Vec<(String, String)>,
backend: TransportBackend,
proxy_url: Option<&str>,
) -> Result<(SplitSink<BoxedWsTransport, Message>, MessageReader), TransportError>
pub async fn connect_with_server( url: &str, headers: Vec<(String, String)>, backend: TransportBackend, proxy_url: Option<&str>, ) -> Result<(SplitSink<BoxedWsTransport, Message>, MessageReader), TransportError>
Connect to the server and return the split halves of the active transport.
Dispatches on backend to the matching backend helper. The
TransportBackend::Tungstenite backend is always available; the
TransportBackend::Sockudo requires the transport-sockudo Cargo
feature and uses a custom HTTP/1.1 handshake path for upgrade headers.
When proxy_url is Some, the Tungstenite backend establishes an HTTP
CONNECT tunnel through the proxy before performing the WebSocket
handshake. The Sockudo backend does not yet support proxying and will
return an error if a proxy URL is supplied.
§Errors
Returns a TransportError if the URL is invalid, headers fail to
parse, the TCP / TLS layer cannot be established, the proxy refuses
the tunnel, or the WebSocket handshake is rejected by the peer. When
the Sockudo backend is selected without the transport-sockudo
feature, returns TransportError::Other.
Source§impl WebSocketClientInner
impl WebSocketClientInner
Sourcepub async fn reconnect(&mut self) -> Result<(), TransportError>
pub async fn reconnect(&mut self) -> Result<(), TransportError>
Reconnect with server.
Make a new connection with server. Use the new read and write halves to update self writer and read and heartbeat tasks.
For stream-based clients (created via connect_stream), reconnection is disabled
because the reader is owned by the caller and cannot be replaced. Stream users
should handle disconnections by creating a new connection.
§Errors
Returns an error if:
- The reconnection attempt times out.
- The connection to the server fails.
Trait Implementations§
Source§impl CleanDrop for WebSocketClientInner
Cleanup on drop: aborts background tasks and clears handlers to break reference cycles.
impl CleanDrop for WebSocketClientInner
Cleanup on drop: aborts background tasks and clears handlers to break reference cycles.
Source§fn clean_drop(&mut self)
fn clean_drop(&mut self)
Source§impl Debug for WebSocketClientInner
impl Debug for WebSocketClientInner
Auto Trait Implementations§
impl Freeze for WebSocketClientInner
impl !RefUnwindSafe for WebSocketClientInner
impl Send for WebSocketClientInner
impl Sync for WebSocketClientInner
impl Unpin for WebSocketClientInner
impl UnsafeUnpin for WebSocketClientInner
impl !UnwindSafe for WebSocketClientInner
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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 more