Skip to main content

Client

Struct Client 

Source
pub struct Client<S> { /* private fields */ }
Expand description

WebSocket stream — owns a socket, reader, writer, and buffers.

Handles both plain ws:// and encrypted wss:// connections. The URL scheme determines whether TLS is used — no separate type needed.

§Usage

use nexus_web::ws::Client;
use nexus_web::tls::TlsConfig;

// Plain WebSocket
let mut ws = Client::builder().connect("ws://localhost:8080/ws")?;

// TLS WebSocket (requires 'tls' feature)
let tls = TlsConfig::new()?;
let mut ws = Client::builder().tls(&tls).connect("wss://exchange.com/ws")?;

// Same API for both:
ws.send_text("Hello!")?;
while let Some(msg) = ws.recv()? {
    // ...
}

Implementations§

Source§

impl Client<TcpStream>

Source

pub fn builder() -> ClientBuilder

Create a builder for configuring buffer sizes, socket options, and TLS.

Source§

impl<S> Client<S>

Source

pub fn from_parts(stream: S, reader: FrameReader, writer: FrameWriter) -> Self

Create from pre-existing parts. For testing or custom handshakes.

Source

pub fn is_poisoned(&self) -> bool

Whether the stream is poisoned (I/O error occurred during send).

A poisoned stream should not be reused — the connection may be in an indeterminate state (partial frame written).

Source

pub fn stream(&self) -> &S

Access the underlying stream.

Source

pub fn stream_mut(&mut self) -> &mut S

Mutable access to the underlying stream.

Source

pub fn reader(&self) -> &FrameReader

Access the FrameReader.

Source

pub fn frame_writer(&self) -> &FrameWriter

Access the FrameWriter.

Source§

impl<S: Read + Write> Client<S>

Source

pub fn connect_with(stream: S, url: &str) -> Result<Self, Error>

Connect using a pre-connected socket with default configuration.

IPv6 addresses must use bracket notation: ws://[::1]:8080/path.

Source

pub fn accept(stream: S) -> Result<Self, Error>

Accept an incoming WebSocket connection (server-side).

Source

pub fn recv(&mut self) -> Result<Option<Message<'_>>, Error>

Receive the next message. Reads from the socket as needed.

Returns Ok(None) on EOF, buffer full, or WouldBlock (non-blocking sockets).

Source

pub fn send_text(&mut self, text: &str) -> Result<(), Error>

Send a text message.

Source

pub fn send_binary(&mut self, data: &[u8]) -> Result<(), Error>

Send a binary message.

Source

pub fn send_ping(&mut self, data: &[u8]) -> Result<(), Error>

Send a ping.

Source

pub fn send_pong(&mut self, data: &[u8]) -> Result<(), Error>

Send a pong.

Source

pub fn close(&mut self, code: CloseCode, reason: &str) -> Result<(), Error>

Initiate close handshake.

Auto Trait Implementations§

§

impl<S> Freeze for Client<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Client<S>
where S: RefUnwindSafe,

§

impl<S> Send for Client<S>
where S: Send,

§

impl<S> Sync for Client<S>
where S: Sync,

§

impl<S> Unpin for Client<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for Client<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for Client<S>
where S: UnwindSafe,

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.

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

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

Source§

fn vzip(self) -> V