pub struct Session { /* private fields */ }Expand description
A handle into the websocket session.
This type can be used to send messages into the WebSocket.
It also implements Sink<Message> for integration with sink-based APIs.
Implementations§
Source§impl Session
impl Session
Sourcepub fn with_codec<T, C>(self, codec: C) -> CodecSession<T, C>where
C: MessageCodec<T>,
pub fn with_codec<T, C>(self, codec: C) -> CodecSession<T, C>where
C: MessageCodec<T>,
Wraps this session with codec so it can send typed messages.
Source§impl Session
impl Session
Sourcepub async fn text(&mut self, msg: impl Into<ByteString>) -> Result<(), Closed>
pub async fn text(&mut self, msg: impl Into<ByteString>) -> Result<(), Closed>
Sends text into the WebSocket.
if session.text("Some text").await.is_err() {
// session closed
}Sourcepub async fn binary(&mut self, msg: impl Into<Bytes>) -> Result<(), Closed>
pub async fn binary(&mut self, msg: impl Into<Bytes>) -> Result<(), Closed>
Sends raw bytes into the WebSocket.
if session.binary(&b"some bytes"[..]).await.is_err() {
// session closed
}Sourcepub async fn ping(&mut self, msg: &[u8]) -> Result<(), Closed>
pub async fn ping(&mut self, msg: &[u8]) -> Result<(), Closed>
Pings the client.
For many applications, it will be important to send regular pings to keep track of if the client has disconnected
Ping payloads longer than 125 bytes are truncated to comply with RFC 6455 control frame size limits.
if session.ping(b"").await.is_err() {
// session is closed
}Sourcepub async fn pong(&mut self, msg: &[u8]) -> Result<(), Closed>
pub async fn pong(&mut self, msg: &[u8]) -> Result<(), Closed>
Pongs the client.
Pong payloads longer than 125 bytes are truncated to comply with RFC 6455 control frame size limits.
match msg {
Message::Ping(bytes) => {
let _ = session.pong(&bytes).await;
}
_ => (),
}Sourcepub async fn continuation(&mut self, msg: Item) -> Result<(), Closed>
pub async fn continuation(&mut self, msg: Item) -> Result<(), Closed>
Manually controls sending continuations.
Be wary of this method. Continuations represent multiple frames that, when combined, are presented as a single message. They are useful when the entire contents of a message are not available all at once. However, continuations MUST NOT be interrupted by other Text or Binary messages. Control messages such as Ping, Pong, or Close are allowed to interrupt a continuation.
Continuations must be initialized with a First variant, and must be terminated by a Last variant, with only Continue variants sent in between.
session.continuation(Item::FirstText("Hello".into())).await?;
session.continuation(Item::Continue(b", World"[..].into())).await?;
session.continuation(Item::Last(b"!"[..].into())).await?;Sourcepub async fn close(self, reason: Option<CloseReason>) -> Result<(), Closed>
pub async fn close(self, reason: Option<CloseReason>) -> Result<(), Closed>
Sends a close message, and consumes the session.
All clones will return Err(Closed) if used after this call.
Close reason descriptions longer than 123 bytes are truncated to comply with RFC 6455 control frame size limits.
session.close(None).awaitTrait Implementations§
Source§impl Sink<Message> for Session
impl Sink<Message> for Session
Source§fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Sink to receive a value. Read moreSource§fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
poll_ready which returned Poll::Ready(Ok(())). Read more