WebSocketClientInner

Struct WebSocketClientInner 

Source
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

Source

pub async fn new_with_writer( config: WebSocketConfig, writer: SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>, ) -> Result<Self, Error>

Create an inner websocket client with an existing writer.

§Errors

Returns an error if the exponential backoff configuration is invalid.

Source

pub async fn connect_url(config: WebSocketConfig) -> Result<Self, Error>

Create an inner websocket client.

§Errors

Returns an error if:

  • The connection to the server fails.
  • The exponential backoff configuration is invalid.
Source

pub async fn connect_with_server( url: &str, headers: Vec<(String, String)>, ) -> Result<(SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>, MessageReader), Error>

Connects with the server creating a tokio-tungstenite websocket stream. Turmoil version that uses the lower-level client_async API with injected stream.

§Errors

Returns an error if:

  • The URL cannot be parsed into a valid client request.
  • The URL is missing a hostname.
  • Header values are invalid.
  • The TCP connection fails.
  • TLS setup fails (for wss:// URLs).
  • The WebSocket handshake fails.
Source

pub async fn reconnect(&mut self) -> Result<(), Error>

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.
Source

pub fn is_alive(&self) -> bool

Check if the client is still connected.

The client is connected if the read task has not finished. It is expected that in case of any failure client or server side. The read task will be shutdown or will receive a Close frame which will finish it. There might be some delay between the connection being closed and the client detecting.

Trait Implementations§

Source§

impl CleanDrop for WebSocketClientInner

Cleanup on drop: aborts background tasks and clears handlers to break reference cycles.

Source§

fn clean_drop(&mut self)

Perform custom cleanup, releasing external resources and breaking strong reference cycles.
Source§

impl Debug for WebSocketClientInner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for WebSocketClientInner

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Ungil for T
where T: Send,