pub struct Client { /* private fields */ }Expand description
A connected socket.io client (one namespace). Cheap to clone; all clones are one client.
Dropping the last clone closes the connection. Handlers get a clone for the duration of the call.
Implementations§
Source§impl Client
impl Client
Sourcepub async fn reconnect(&self) -> Result<()>
pub async fn reconnect(&self) -> Result<()>
Connects again after the client closed (a ConnectRejected, ReconnectFailed, a
server disconnect without reconnect_on_disconnect, or disconnect()), with the same
handlers and options; auth_with and connect_params_with run again, so a new token
can be handed in first. Resolves like connect(): once the server accepted the
namespace, or with the error of that attempt (the client then stays closed). A no-op
while the client is not closed. Must not be called from one of its handlers.
Sourcepub fn emit(
&self,
event: impl AsRef<str>,
data: impl Into<Payload>,
) -> Result<()>
pub fn emit( &self, event: impl AsRef<str>, data: impl Into<Payload>, ) -> Result<()>
Sends event with data (Payload, serde_json::Value, a string, bytes, () for
no arguments). Never blocks: the packet is queued for the connection task.
Fails with Error::NotConnected while disconnected (unless
buffer_while_disconnected), Error::Closed after the client closed, and
Error::ReservedEvent for connect, disconnect, …
Sourcepub async fn emit_with_ack(
&self,
event: impl AsRef<str>,
data: impl Into<Payload>,
timeout: Duration,
) -> Result<Payload>
pub async fn emit_with_ack( &self, event: impl AsRef<str>, data: impl Into<Payload>, timeout: Duration, ) -> Result<Payload>
Sends event and waits for the server’s ack (socket.io-client
socket.timeout(ms).emitWithAck(...)). Resolves with the ack’s arguments, or fails with
Error::AckTimeout after timeout, Error::Disconnected when the connection drops
first. Dropping the future forgets the ack.
Sourcepub fn on<M>(
&self,
event: impl Into<String>,
handler: impl Handler<Event, M>,
) -> ListenerId
pub fn on<M>( &self, event: impl Into<String>, handler: impl Handler<Event, M>, ) -> ListenerId
Adds a handler for event; returns its id for off_listener.
Sourcepub fn once<M>(
&self,
event: impl Into<String>,
handler: impl Handler<Event, M>,
) -> ListenerId
pub fn once<M>( &self, event: impl Into<String>, handler: impl Handler<Event, M>, ) -> ListenerId
Adds a handler for the next event only.
Sourcepub fn on_any<M>(&self, handler: impl Handler<Event, M>) -> ListenerId
pub fn on_any<M>(&self, handler: impl Handler<Event, M>) -> ListenerId
Adds a handler for every event (socket.io-client onAny).
Sourcepub fn on_state_change<M>(
&self,
handler: impl Handler<ConnectionState, M>,
) -> ListenerId
pub fn on_state_change<M>( &self, handler: impl Handler<ConnectionState, M>, ) -> ListenerId
Adds a handler for every ConnectionState transition.
Sourcepub fn off_listener(&self, id: ListenerId) -> bool
pub fn off_listener(&self, id: ListenerId) -> bool
Removes one handler; false when it was gone already (a once handler that ran).
Sourcepub fn listener_count(&self, event: &str) -> usize
pub fn listener_count(&self, event: &str) -> usize
Number of handlers of event (socket.io-client listeners(event).length).
Sourcepub fn state(&self) -> ConnectionState
pub fn state(&self) -> ConnectionState
The current state.
Sourcepub fn state_watch(&self) -> Receiver<ConnectionState>
pub fn state_watch(&self) -> Receiver<ConnectionState>
A watch of the state: changed().await wakes on transitions (a slow reader sees the
latest; use on_state_change to see every one).
Sourcepub async fn wait_for(
&self,
predicate: impl Fn(&ConnectionState) -> bool,
) -> ConnectionState
pub async fn wait_for( &self, predicate: impl Fn(&ConnectionState) -> bool, ) -> ConnectionState
Waits until the state matches predicate (or the client closed).
Sourcepub fn recovered(&self) -> bool
pub fn recovered(&self) -> bool
The last connect restored the session (connection state recovery, socket.recovered).
Sourcepub fn link_info(&self) -> LinkInfo
pub fn link_info(&self) -> LinkInfo
A snapshot of the link: transport, sids, heartbeat, last packet, attempts.
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Leaves the namespace (DISCONNECT packet), closes the connection and stops reconnecting.
Bounded: the goodbye (queued packets, DISCONNECT, engine.io close) gets at most 2 s on a dead link. Events already received are still handled; handlers get up to 5 s in total, then the event task is stopped. When it returns no handler starts any more. Safe to call from inside a handler (it then does not wait for the handlers; the event task ends after the running handler).