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: Streamit 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>>
impl WebSocket<CLIENT, BufReader<TcpStream>>
sourcepub async fn connect<A>(addr: A, path: impl AsRef<str>) -> Result<Self>where
A: ToSocketAddrs + Display,
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.
sourcepub async fn connect_with_headers(
addr: impl ToSocketAddrs + Display,
path: impl AsRef<str>,
headers: impl IntoIterator<Item = impl Header>
) -> Result<Self>
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>>>
impl WebSocket<CLIENT, BufReader<TlsStream<TcpStream>>>
sourcepub async fn connect<A>(addr: A, path: impl AsRef<str>) -> Result<Self>where
A: ToSocketAddrs + Display,
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.
sourcepub async fn connect_with_headers(
addr: impl ToSocketAddrs + Display,
path: impl AsRef<str>,
headers: impl IntoIterator<Item = impl Header>
) -> Result<Self>
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>
impl<RW: Unpin + AsyncBufRead + AsyncWrite> WebSocket<CLIENT, RW>
source§impl<RW: Unpin + AsyncBufRead + AsyncWrite> WebSocket<SERVER, RW>
impl<RW: Unpin + AsyncBufRead + AsyncWrite> WebSocket<SERVER, RW>
source§impl<const SIDE: bool, W: Unpin + AsyncWrite> WebSocket<SIDE, W>
impl<const SIDE: bool, W: Unpin + AsyncWrite> WebSocket<SIDE, W>
sourcepub async fn send(&mut self, msg: impl Message) -> Result<()>
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?;
sourcepub async fn flash(&mut self) -> Result<()>
pub async fn flash(&mut self) -> Result<()>
Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.
sourcepub async fn close<T>(self, reason: T) -> Result<()>where
T: CloseFrame,
T::Frame: AsRef<[u8]>,
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?;