Struct fastwebsockets::WebSocket

source ·
pub struct WebSocket<S> { /* private fields */ }
Expand description

WebSocket protocol implementation over an async stream.

Implementations§

source§

impl<'f, S> WebSocket<S>

source

pub fn after_handshake(stream: S, role: Role) -> Self
where S: AsyncRead + AsyncWrite + Unpin,

Creates a new WebSocket from a stream that has already completed the WebSocket handshake.

Use the upgrade feature to handle server upgrades and client handshakes.

§Example
use tokio::net::TcpStream;
use fastwebsockets::{WebSocket, OpCode, Role};
use anyhow::Result;

async fn handle_client(
  socket: TcpStream,
) -> Result<()> {
  let mut ws = WebSocket::after_handshake(socket, Role::Server);
  // ...
  Ok(())
}
source

pub fn into_inner(self) -> S

Consumes the WebSocket and returns the underlying stream.

source

pub fn set_writev(&mut self, vectored: bool)

Sets whether to use vectored writes. This option does not guarantee that vectored writes will be always used.

Default: true

source

pub fn set_writev_threshold(&mut self, threshold: usize)

source

pub fn set_auto_close(&mut self, auto_close: bool)

Sets whether to automatically close the connection when a close frame is received. When set to false, the application will have to manually send close frames.

Default: true

source

pub fn set_auto_pong(&mut self, auto_pong: bool)

Sets whether to automatically send a pong frame when a ping frame is received.

Default: true

source

pub fn set_max_message_size(&mut self, max_message_size: usize)

Sets the maximum message size in bytes. If a message is received that is larger than this, the connection will be closed.

Default: 64 MiB

source

pub fn set_auto_apply_mask(&mut self, auto_apply_mask: bool)

Sets whether to automatically apply the mask to the frame payload.

Default: true

source

pub fn is_closed(&self) -> bool

source

pub async fn write_frame( &mut self, frame: Frame<'f>, ) -> Result<(), WebSocketError>
where S: AsyncRead + AsyncWrite + Unpin,

Writes a frame to the stream.

§Example
use fastwebsockets::{WebSocket, Frame, OpCode};
use tokio::net::TcpStream;
use anyhow::Result;

async fn send(
  ws: &mut WebSocket<TcpStream>
) -> Result<()> {
  let mut frame = Frame::binary(vec![0x01, 0x02, 0x03].into());
  ws.write_frame(frame).await?;
  Ok(())
}
source

pub async fn read_frame(&mut self) -> Result<Frame<'f>, WebSocketError>
where S: AsyncRead + AsyncWrite + Unpin,

Reads a frame from the stream.

This method will unmask the frame payload. For fragmented frames, use FragmentCollector::read_frame.

Text frames payload is guaranteed to be valid UTF-8.

§Example
use fastwebsockets::{OpCode, WebSocket, Frame};
use tokio::net::TcpStream;
use anyhow::Result;

async fn echo(
  ws: &mut WebSocket<TcpStream>
) -> Result<()> {
  let frame = ws.read_frame().await?;
  match frame.opcode {
    OpCode::Text | OpCode::Binary => {
      ws.write_frame(frame).await?;
    }
    _ => {}
  }
  Ok(())
}

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<S> UnwindSafe for WebSocket<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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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