Skip to main content

WsSession

Struct WsSession 

Source
pub struct WsSession { /* private fields */ }
Expand description

An established JMAP WebSocket session (RFC 8887).

Call next_frame in a loop to receive events. Use send_request to transmit JMAP requests.

The caller is responsible for reconnecting after the stream ends or returns a transport error. Use exponential backoff.

§Drop and cancellation (bd:JMAP-6r7c.24)

WsSession may be dropped at any point — including from inside a tokio::select! losing branch while next_frame is awaiting. Dropping is always safe and always synchronous:

  • Partial frame state is discarded. The split sink and stream halves are owned by the session; dropping the session drops both halves and any in-flight tungstenite buffering.
  • The underlying TCP / TLS connection is closed. No Close frame is sent — dropping skips the WebSocket close handshake. Callers that want the server to see a clean shutdown should arrange to receive the server’s Close (which next_frame returns as None) before drop; this crate does not currently expose an explicit client-initiated close() method.
  • Resumption is the caller’s job. If you need to recover the conversation state, you must either persist enough application-level state to replay, or rely on the server’s push-state replay protocol. WsSession itself buffers nothing the caller can replay.

next_frame is cancel-safe in the sense that cancelling its future (via select! or drop) does not corrupt subsequent calls to next_frame on the same WsSession — but cancelling means giving up on any partial frame that was being read; the next next_frame call starts from the next complete message.

Implementations§

Source§

impl WsSession

Source

pub async fn next_frame(&mut self) -> Option<Result<WsFrame, ClientError>>

Receive the next parsed frame from the server.

Delegates to WsReceiver::next_frame. Use this unified handle when a single task drives both send and receive; for the receive-loop-in-a-separate-task topology, call split to get owned WsReceiver / WsSender halves.

Source

pub async fn send_text(&mut self, text: String) -> Result<(), ClientError>

Send a raw text frame over the WebSocket connection.

Delegates to WsSender::send_text.

Source

pub async fn send_request( &mut self, req: &JmapRequest, id: Option<&str>, ) -> Result<(), ClientError>

Send a JMAP request over the WebSocket connection.

Delegates to WsSender::send_request.

Source

pub fn split(self) -> (WsSender, WsReceiver)

Consume the session and return its owned send and receive halves (bd:JMAP-6r7c.31).

Use this when a caller needs to drive the receive loop and the send path concurrently — typically from two tokio::spawn-ed tasks, one running a while let Some(...) = receiver.next_frame() .await loop and one occasionally sending requests via sender.send_request(...). The unified WsSession API requires &mut self for both directions and therefore cannot service them concurrently; split is the explicit opt-in.

The two halves are independent owners of their tungstenite sub-streams. Dropping one half does not close the connection until the other half is also dropped (tungstenite’s WebSocketStream only initiates the close handshake when both halves are gone). To initiate a clean shutdown from a split session, drop the sender after sending a final request and read from the receiver until it returns None.

Trait Implementations§

Source§

impl Debug for WsSession

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more