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>
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
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.
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?;
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl !UnwindSafe for Session
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)