Skip to main content

Socket

Struct Socket 

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

One connected client.

Cheap to clone and safe to keep: sending goes through a channel, so a handle stored in a room registry does not hold the connection’s task or its buffers. Sending to a socket that has gone away is an error, not a panic.

ⓘ
socket.send("hello").await?;
socket.send_json(&Update { count: 3 }).await?;
socket.close().await;

Implementations§

Source§

impl Socket

Source

pub fn new( id: SocketId, path: impl Into<Arc<str>>, params: Vec<(String, String)>, outbound: UnboundedSender<Outbound>, ) -> Socket

Build a handle over outbound.

The transport owns the receiving half; a handler is only ever given this side.

Source

pub fn id(&self) -> SocketId

This connection’s id.

Source

pub fn path(&self) -> &str

The path it connected to.

Source

pub fn param(&self, name: &str) -> Option<&str>

A route parameter captured from the path — rooms/{room} gives room.

Source

pub fn parse_param<T>(&self, name: &str) -> Result<T, Error>
where T: FromStr, <T as FromStr>::Err: Display,

A route parameter, parsed.

A bad request when it will not parse: the value came from the URL the client asked for.

Source

pub fn params(&self) -> &[(String, String)]

Every captured parameter.

Source

pub fn send(&self, message: impl Into<Message>) -> Result<(), Error>

Send a frame.

Queued rather than written: a handler that awaited the socket would block on a slow client, and one slow client must not hold up the task reading everyone else’s messages.

Source

pub fn send_json(&self, value: &impl Serialize) -> Result<(), Error>

Send a value as a JSON text frame.

Source

pub fn close(&self) -> Result<(), Error>

Close the connection.

Idempotent: closing twice is not an error, because two halves of a handler deciding to close at once is normal.

Source

pub fn close_with(&self, reason: impl Into<String>) -> Result<(), Error>

Close with a reason the peer will see.

Source

pub fn is_closed(&self) -> bool

Whether this handle has been asked to close, or the connection has gone.

Trait Implementations§

Source§

impl Clone for Socket

Source§

fn clone(&self) -> Socket

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Socket

Source§

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

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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