pub struct WebsocketServer { /* private fields */ }Expand description
Wraps an embedded_websocket::WebSocketServer
Implementations§
Source§impl WebsocketServer
impl WebsocketServer
Sourcepub 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>
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 onpb.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.
Sourcepub fn from_http(
pb: PBufRdWr<'_>,
subprotocol: Option<&WebSocketSubProtocol>,
max_msg_len: usize,
max_aux_len: usize,
) -> Result<Option<Self>, Error>
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.
Sourcepub fn from_wss(
ws: WebSocketServer,
max_msg_len: usize,
max_aux_len: usize,
) -> Self
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.
Sourcepub fn send_text(&mut self, pb: PBufRdWr<'_>, data: &str) -> Result<(), Error>
pub fn send_text(&mut self, pb: PBufRdWr<'_>, data: &str) -> Result<(), Error>
Send an unfragmented websocket text message
Sourcepub fn send_binary(
&mut self,
pb: PBufRdWr<'_>,
data: &[u8],
) -> Result<(), Error>
pub fn send_binary( &mut self, pb: PBufRdWr<'_>, data: &[u8], ) -> Result<(), Error>
Send an unfragmented websocket binary message
Sourcepub fn send(
&mut self,
pb: PBufRdWr<'_>,
msg: WebSocketSendMessageType,
eom: bool,
data: &[u8],
) -> Result<(), Error>
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.
Sourcepub fn receive(
&mut self,
pb: PBufRdWr<'_>,
message: PBufWr<'_>,
is_text: &mut bool,
) -> Result<bool, Error>
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.