pub struct WebSocket { /* private fields */ }Expand description
A live WebSocket connection.
Control frames are handled by the runtime: pings are answered with pongs
automatically and never surface, and a close frame ends recv rather
than appearing as a message. Handlers therefore only ever see the text and
binary messages the peer actually sent.
Implementations§
Source§impl WebSocket
impl WebSocket
Sourcepub async fn recv(&mut self) -> Option<Result<Message>>
pub async fn recv(&mut self) -> Option<Result<Message>>
Receive the next message.
Returns None once the peer has closed the connection — cleanly or
otherwise. Ping, pong and close frames are consumed internally, and
keepalive pings are sent from here while waiting.
Sourcepub async fn send_binary<B: Into<Bytes>>(&mut self, bytes: B) -> Result<()>
pub async fn send_binary<B: Into<Bytes>>(&mut self, bytes: B) -> Result<()>
Send a binary message.
Sourcepub async fn send_json<T: Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<()>
pub async fn send_json<T: Serialize + ?Sized>( &mut self, value: &T, ) -> Result<()>
Serialise value as JSON and send it as text.
Sourcepub async fn close(self, code: CloseCode, reason: &str) -> Result<()>
pub async fn close(self, code: CloseCode, reason: &str) -> Result<()>
Close the connection, completing the RFC 6455 closing handshake.
Sends the close frame, then reads until the peer’s own close arrives or
[CLOSE_HANDSHAKE_TIMEOUT] passes — a peer that never replies must not
hold the task open, but one that replies promptly should see a clean
close rather than a TCP reset.
Consumes the socket: after a close there is nothing left to do with it.
Sourcepub fn split(self) -> (WebSocketSender, WebSocketReceiver)
pub fn split(self) -> (WebSocketSender, WebSocketReceiver)
Split into independent sending and receiving halves.
Needed whenever one task pushes messages while another reads them — a notification feed, say, that must keep draining the socket so pings are answered.
The halves are plumbing, not policy: keepalive pings and the route’s
[json] check both live on the whole socket and do not survive a
split. A split socket answers the peer’s pings (tungstenite does that
on read) but sends none of its own, and its close handshake must be
completed by calling WebSocketSender::close once recv ends.
Auto Trait Implementations§
impl !Freeze for WebSocket
impl !RefUnwindSafe for WebSocket
impl !Sync for WebSocket
impl !UnwindSafe for WebSocket
impl Send for WebSocket
impl Unpin for WebSocket
impl UnsafeUnpin 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
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more