Struct WebsocketServer

Source
pub struct WebsocketServer { /* private fields */ }
Expand description

Implementations§

Source§

impl WebsocketServer

Source

pub fn from_http_scan( pb: PBufRdWr<'_>, subprotocol: Option<&WebSocketSubProtocol>, max_msg_len: usize, max_aux_len: usize, header_cb: impl FnMut(&str, &[u8]), ) -> Result<Option<Self>, Error>

Attempt to interpret the initial data in the given pipe-buffer stream as websocket HTTP headers and initialise the websocket stream from them.

Returns:

  • Ok(None) if more data is required

  • Ok(Some(Self)) if valid HTTP websocket headers were found and consumed and the websocket is now ready. A protocol reply will have been sent back on pb.wr.

  • Err(_) if the HTTP headers are invalid, or contain invalid data for a websocket stream. All the initial data will be left unconsumed in the pipe buffer in case it can be interpreted as another protocol

subprotocol argument may be used to specify a subprotocol to pass back to the client, if required. See embedded_websocket documentation.

max_msg_len puts a limit on the size of data that will be allowed in the message buffer before failing the websocket, as a protection against denial of service attacks. This is the limit of how much unread data is allowed in that buffer. If the caller streams the data out as it is read, then an unlimited amount of data may still be received. In case of exceeding this limit, Error::WriteToBufferTooSmall is returned.

max_aux_len puts a limit on the size of data associated with Ping and Close messages before failing the websocket, as a protection against denial of service attacks. In case of exceeding this limit, Error::WriteToBufferTooSmall is returned.

header_cb is called for each HTTP header line as header_cb(field_name, field_value) once the websocket connection has been verified in order to allow the caller to extract whatever details may be required, such as Origin.

Source

pub fn from_http( pb: PBufRdWr<'_>, subprotocol: Option<&WebSocketSubProtocol>, max_msg_len: usize, max_aux_len: usize, ) -> Result<Option<Self>, Error>

Attempt to interpret the initial data in the given pipe-buffer stream as websocket HTTP headers and initialise the websocket stream from them.

See WebsocketServer::from_http_scan for details of arguments and returns.

Source

pub fn from_wss( ws: WebSocketServer, max_msg_len: usize, max_aux_len: usize, ) -> Self

Create from an already-initialised WebSocketServer

max_msg_len puts a limit on the size of data that will be allowed in the message buffer before failing the websocket, as a protection against denial of service attacks. This is the limit of how much unread data is allowed in that buffer. If the caller streams the data out as it is read, then an unlimited amount of data may still be received. In case of exceeding this limit, Error::WriteToBufferTooSmall is returned.

max_aux_len puts a limit on the size of data associated with Ping and Close messages before failing the websocket, as a protection against denial of service attacks. In case of exceeding this limit, Error::WriteToBufferTooSmall is returned.

Source

pub fn send_text(&mut self, pb: PBufRdWr<'_>, data: &str) -> Result<(), Error>

Send an unfragmented websocket text message

Source

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

Send an unfragmented websocket binary message

Source

pub fn send( &mut self, pb: PBufRdWr<'_>, msg: WebSocketSendMessageType, eom: bool, data: &[u8], ) -> Result<(), Error>

Send an arbitrary websocket message. This is a wrapper around WebSocketServer::write. For an unfragmented message, eom should be true. For a fragmented message, it should be true only for the final fragment.

Source

pub fn receive( &mut self, pb: PBufRdWr<'_>, message: PBufWr<'_>, is_text: &mut bool, ) -> Result<bool, Error>

Process as much data as possible from the stream. Whilst processing, sends back Pong and CloseReply messages as necessary according to protocol. If the stream is closed at a websocket protocol level, closes the output stream pb.wr.

Received message data is streamed into the message pipe-buffer. When the end of the message is reached, EOF is indicated on the pipe-buffer (with state Closing). This takes care of websocket fragments being used to stream data. The caller may wait for an EOF and process the entire message, or else process the data as it comes in (streaming style). Even partial fragments may result in data being added to the pipe-buffer, so you can’t count on seeing data with the original fragment boundaries. *is_text will be set according to the message type: true for text, false for binary. When EOF is indicated on the message pipe-buffer, the caller must process the contents and reset the buffer (with PipeBuf::reset()) before calling this method again, so that a new message can be read into it.

Returns Ok(true) if there was activity, Ok(false) if it is not possible to advance right now, or Err(_) in case of protocol or limit errors. After each call check to see whether a partial or complete message was received. In case of EOF on a message, there may be more websocket frames still to read, so call again.

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.

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.