pub struct Session { /* private fields */ }
Expand description
A handle into the websocket session.
This type can be used to send messages into the websocket.
Implementations§
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>
Send 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>
Send 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>
Ping the client
For many applications, it will be important to send regular pings to keep track of if the client has disconnected
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>
Pong the client
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 control 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?;