Struct web_socket::WebSocket

source ·
pub struct WebSocket<const SIDE: bool, Stream> {
    pub stream: Stream,
    pub on_event: Box<dyn FnMut(Event<'_>) -> Result<(), Box<dyn Error + Send + Sync>> + Send + Sync>,
    /* private fields */
}
Expand description

WebSocket implementation for both client and server

Fields§

§stream: Stream

it is a low-level abstraction that represents the underlying byte stream over which WebSocket messages are exchanged.

§on_event: Box<dyn FnMut(Event<'_>) -> Result<(), Box<dyn Error + Send + Sync>> + Send + Sync>

Listen for incoming websocket Event.

Example
use web_socket::{client::WS, Event};

let mut ws = WS::connect("localhost:80", "/").await?;
// Fire when received ping/pong frame.
ws.on_event = Box::new(|ev| {
    println!("{ev:?}");
    Ok(())
});

Implementations§

source§

impl WebSocket<CLIENT, BufReader<TcpStream>>

source

pub async fn connect<A>(addr: A, path: impl AsRef<str>) -> Result<Self>where A: ToSocketAddrs + Display,

establishe a websocket connection to a remote address.

source

pub async fn connect_with_headers( addr: impl ToSocketAddrs + Display, path: impl AsRef<str>, headers: impl IntoIterator<Item = impl Header> ) -> Result<Self>

establishes a connection with headers

source§

impl WebSocket<CLIENT, BufReader<TlsStream<TcpStream>>>

source

pub async fn connect<A>(addr: A, path: impl AsRef<str>) -> Result<Self>where A: ToSocketAddrs + Display,

establishe a secure websocket connection to a remote address.

source

pub async fn connect_with_headers( addr: impl ToSocketAddrs + Display, path: impl AsRef<str>, headers: impl IntoIterator<Item = impl Header> ) -> Result<Self>

establishes a secure connection with headers

source§

impl<RW: Unpin + AsyncBufRead + AsyncWrite> WebSocket<CLIENT, RW>

source

pub async fn recv(&mut self) -> Result<Data<'_, RW>>

reads Data from websocket stream.

source§

impl<Stream> WebSocket<SERVER, Stream>

source

pub fn new(stream: Stream) -> Self

Create a new websocket instance.

source§

impl<RW: Unpin + AsyncBufRead + AsyncWrite> WebSocket<SERVER, RW>

source

pub async fn recv(&mut self) -> Result<Data<'_, RW>>

reads Data from websocket stream.

source§

impl<const SIDE: bool, W: Unpin + AsyncWrite> WebSocket<SIDE, W>

source

pub async fn send(&mut self, msg: impl Message) -> Result<()>

Send message to a endpoint by writing it to a WebSocket stream.

Example
use web_socket::{client::WS, CloseCode, Event};

let mut ws = WS::connect("localhost:80", "/").await?;
ws.send("Text Message").await?;
ws.send(b"Binary Data").await?;

// You can also send control frame.
ws.send(Event::Ping(b"Hello!")).await?;
ws.send(Event::Pong(b"Hello!")).await?;
source

pub async fn flash(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.

source

pub async fn close<T>(self, reason: T) -> Result<()>where T: CloseFrame, T::Frame: AsRef<[u8]>,

  • The Close frame MAY contain a body that indicates a reason for closing.
Example
use web_socket::{client::WS, CloseCode};

let ws = WS::connect("localhost:80", "/").await?;
ws.close((CloseCode::Normal, "Closed successfully")).await?;

Trait Implementations§

source§

impl<const SIDE: bool, Stream> From<Stream> for WebSocket<SIDE, Stream>

source§

fn from(stream: Stream) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<const SIDE: bool, Stream> !RefUnwindSafe for WebSocket<SIDE, Stream>

§

impl<const SIDE: bool, Stream> Send for WebSocket<SIDE, Stream>where Stream: Send,

§

impl<const SIDE: bool, Stream> Sync for WebSocket<SIDE, Stream>where Stream: Sync,

§

impl<const SIDE: bool, Stream> Unpin for WebSocket<SIDE, Stream>where Stream: Unpin,

§

impl<const SIDE: bool, Stream> !UnwindSafe for WebSocket<SIDE, Stream>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

const: unstable · source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<T> for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.