Struct tungstenite::protocol::WebSocket
source · pub struct WebSocket<Stream> { /* private fields */ }
Expand description
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.
Implementations
sourceimpl<Stream> WebSocket<Stream>
impl<Stream> WebSocket<Stream>
sourcepub fn from_raw_socket(
stream: Stream,
role: Role,
config: Option<WebSocketConfig>
) -> Self
pub fn from_raw_socket(
stream: Stream,
role: Role,
config: Option<WebSocketConfig>
) -> Self
Convert 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()
or accept()
functions of the crate to construct a websocket.
sourcepub fn from_partially_read(
stream: Stream,
part: Vec<u8>,
role: Role,
config: Option<WebSocketConfig>
) -> Self
pub fn from_partially_read(
stream: Stream,
part: Vec<u8>,
role: Role,
config: Option<WebSocketConfig>
) -> Self
Convert 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()
or accept()
functions of the crate to construct a websocket.
sourcepub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig))
pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig))
Change the configuration.
sourceimpl<Stream: Read + Write> WebSocket<Stream>
impl<Stream: Read + Write> WebSocket<Stream>
sourcepub fn read_message(&mut self) -> Result<Message>
pub fn read_message(&mut self) -> Result<Message>
Read a message from stream, if possible.
This function sends pong and close responses automatically. However, it never blocks on write.
sourcepub fn write_message(&mut self, message: Message) -> Result<()>
pub fn write_message(&mut self, message: Message) -> Result<()>
Send a message to stream, if possible.
WebSocket will buffer a configurable number of messages at a time, except to reply to Ping
and Close requests. If the WebSocket’s send queue is full, SendQueueFull
will be returned
along with the passed message. Otherwise, the message is queued and Ok(()) is returned.
Note that only the last pong frame is stored to be sent, and only the most recent pong frame is sent if multiple pong frames are queued.
sourcepub fn close(&mut self, code: Option<CloseFrame<'_>>) -> Result<()>
pub fn close(&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.
sourcepub fn write_pending(&mut self) -> Result<()>
pub fn write_pending(&mut self) -> Result<()>
Flush the pending send queue.