pub struct WebSocket { /* private fields */ }Expand description
A WebSocket connection (server-side).
Notes:
- Server -> client frames are not masked.
- Client -> server frames must be masked (enforced).
Implementations§
Source§impl WebSocket
impl WebSocket
Sourcepub fn new(stream: TcpStream, buffered: Vec<u8>) -> Self
pub fn new(stream: TcpStream, buffered: Vec<u8>) -> Self
Create a websocket from a TCP stream and an optional prefix of already-buffered bytes.
Sourcepub async fn read_frame(&mut self) -> Result<Frame, WebSocketError>
pub async fn read_frame(&mut self) -> Result<Frame, WebSocketError>
Read the next frame.
Sourcepub async fn write_frame(&mut self, frame: &Frame) -> Result<(), WebSocketError>
pub async fn write_frame(&mut self, frame: &Frame) -> Result<(), WebSocketError>
Write a frame to the peer (server-side, unmasked).
Sourcepub async fn read_text(&mut self) -> Result<String, WebSocketError>
pub async fn read_text(&mut self) -> Result<String, WebSocketError>
Convenience: read a text message.
Sourcepub async fn read_text_or_close(
&mut self,
) -> Result<Option<String>, WebSocketError>
pub async fn read_text_or_close( &mut self, ) -> Result<Option<String>, WebSocketError>
Convenience: read a text message, transparently handling ping/pong/close.
Behavior:
Pingframes are answered withPong(same payload) and ignored.Pongframes are ignored.Closeframes are replied to with aCloseecho and returnOk(None).- Any non-text data frame returns a protocol error.
Sourcepub async fn send_pong(&mut self, payload: &[u8]) -> Result<(), WebSocketError>
pub async fn send_pong(&mut self, payload: &[u8]) -> Result<(), WebSocketError>
Send a Pong control frame (server-side, unmasked).
Sourcepub async fn send_text(&mut self, text: &str) -> Result<(), WebSocketError>
pub async fn send_text(&mut self, text: &str) -> Result<(), WebSocketError>
Convenience: send a text message.
Sourcepub async fn send_bytes(&mut self, data: &[u8]) -> Result<(), WebSocketError>
pub async fn send_bytes(&mut self, data: &[u8]) -> Result<(), WebSocketError>
Convenience: send a binary message.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for WebSocket
impl RefUnwindSafe for WebSocket
impl Send for WebSocket
impl Sync for WebSocket
impl Unpin for WebSocket
impl UnwindSafe for WebSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).