Struct WebSocket
struct WebSocket<Stream> { ... }
WebSocket input-output stream.
This is THE structure you want to create to be able to speak the WebSocket protocol.
It may be created by calling connect, accept or client functions.
Use WebSocket::read, WebSocket::send to received and send messages.
Implementations
impl<Stream> WebSocket<Stream>
fn from_raw_socket(stream: Stream, role: Role, config: Option<WebSocketConfig>) -> SelfConvert a raw socket into a WebSocket without performing a handshake.
Call this function if you're using Tungstenite as a part of a web framework or together with an existing one. If you need an initial handshake, use
connect()oraccept()functions of the crate to construct a websocket.Panics
Panics if config is invalid e.g.
max_write_buffer_size <= write_buffer_size.fn from_partially_read(stream: Stream, part: Vec<u8>, role: Role, config: Option<WebSocketConfig>) -> SelfConvert a raw socket into a WebSocket without performing a handshake.
Call this function if you're using Tungstenite as a part of a web framework or together with an existing one. If you need an initial handshake, use
connect()oraccept()functions of the crate to construct a websocket.Panics
Panics if config is invalid e.g.
max_write_buffer_size <= write_buffer_size.fn get_ref(self: &Self) -> &StreamReturns a shared reference to the inner stream.
fn get_mut(self: &mut Self) -> &mut StreamReturns a mutable reference to the inner stream.
fn set_config<impl FnOnce(&mut WebSocketConfig): FnOnce(&mut WebSocketConfig)>(self: &mut Self, set_func: impl FnOnce(&mut WebSocketConfig))Change the configuration.
Panics
Panics if config is invalid e.g.
max_write_buffer_size <= write_buffer_size.fn get_config(self: &Self) -> &WebSocketConfigRead the configuration.
fn can_read(self: &Self) -> boolCheck if it is possible to read messages.
Reading is impossible after receiving
Message::Close. It is still possible after sending close frame since the peer still may send some data before confirming close.fn can_write(self: &Self) -> boolCheck if it is possible to write messages.
Writing gets impossible immediately after sending or receiving
Message::Close.
impl<Stream: Read + Write> WebSocket<Stream>
fn read(self: &mut Self) -> Result<Message>Read a message from stream, if possible.
This will also queue responses to ping and close messages. These responses will be written and flushed on the next call to
read,writeorflush.Closing the connection
When the remote endpoint decides to close the connection this will return the close message with an optional close frame.
You should continue calling
read,writeorflushto drive the reply to the close frame untilError::ConnectionClosedis returned. Once that happens it is safe to drop the underlying connection.fn send(self: &mut Self, message: Message) -> Result<()>Writes and immediately flushes a message. Equivalent to calling
writethenflush.fn write(self: &mut Self, message: Message) -> Result<()>Write a message to the provided stream, if possible.
A subsequent call should be made to
flushto flush writes.In the event of stream write failure the message frame will be stored in the write buffer and will try again on the next call to
writeorflush.If the write buffer would exceed the configured
WebSocketConfig::max_write_buffer_sizeErr(WriteBufferFull(msg_frame))is returned.This call will generally not flush. However, if there are queued automatic messages they will be written and eagerly flushed.
For example, upon receiving ping messages tungstenite queues pong replies automatically. The next call to
read,writeorflushwill write & flush the pong reply. This means you should not respond to ping frames manually.You can however send pong frames manually in order to indicate a unidirectional heartbeat as described in RFC 6455. Note that if
readreturns a ping, you shouldflushbefore passing a custom pong towrite, otherwise the automatic queued response to the ping will not be sent as it will be replaced by your custom pong message.Errors
- If the WebSocket's write buffer is full,
Error::WriteBufferFullwill be returned along with the equivalent passed message frame. - If the connection is closed and should be dropped, this will return
Error::ConnectionClosed. - If you try again after
Error::ConnectionClosedwas returned either from here or fromread,Error::AlreadyClosedwill be returned. This indicates a program error on your part. Error::Iois returned if the underlying connection returns an error (consider these fatal except for WouldBlock).Error::Capacityif your message size is bigger than the configured max message size.
- If the WebSocket's write buffer is full,
fn flush(self: &mut Self) -> Result<()>Flush writes.
Ensures all messages previously passed to
writeand automatic queued pong responses are written & flushed into the underlying stream.fn close(self: &mut Self, code: Option<CloseFrame>) -> Result<()>Close the connection.
This function guarantees that the close frame will be queued. There is no need to call it again. Calling this function is the same as calling
write(Message::Close(..)).After queuing the close frame you should continue calling
readorflushto drive the close handshake to completion.The websocket RFC defines that the underlying connection should be closed by the server. Tungstenite takes care of this asymmetry for you.
When the close handshake is finished (we have both sent and received a close message),
readorflushwill return [Error::ConnectionClosed] if this endpoint is the server.If this endpoint is a client, [Error::ConnectionClosed] will only be returned after the server has closed the underlying connection.
It is thus safe to drop the underlying connection as soon as [Error::ConnectionClosed] is returned from
readorflush.fn read_message(self: &mut Self) -> Result<Message>Old name for
read.fn write_message(self: &mut Self, message: Message) -> Result<()>Old name for
send.fn write_pending(self: &mut Self) -> Result<()>Old name for
flush.
impl<Stream> Freeze for WebSocket<Stream>
impl<Stream> RefUnwindSafe for WebSocket<Stream>
impl<Stream> Send for WebSocket<Stream>
impl<Stream> Sync for WebSocket<Stream>
impl<Stream> Unpin for WebSocket<Stream>
impl<Stream> UnwindSafe for WebSocket<Stream>
impl<Stream: $crate::fmt::Debug> Debug for WebSocket<Stream>
fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result
impl<T> Any for WebSocket<Stream>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for WebSocket<Stream>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for WebSocket<Stream>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for WebSocket<Stream>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> Same for WebSocket<Stream>
impl<T, U> Into for WebSocket<Stream>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for WebSocket<Stream>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for WebSocket<Stream>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<V, T> VZip for WebSocket<Stream>
fn vzip(self: Self) -> V