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>, ) -> Self

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 identity(&self) -> String

This connection’s id, qualified so it is unique across replicas.

This is what to send the browser, and what it should echo back in X-Socket-ID so to_others can skip it:

ⓘ
async fn on_connect(&self, socket: &Socket) -> Result<()> {
    socket.send_json(&json!({ "socket_id": socket.identity() })).await
}

Sending the bare id instead works on one replica and fails quietly on two: every replica has a socket 7, so “everyone except 7” would silence an unrelated browser on each of the others.

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>
where T: FromStr, T::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<()>

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<()>

Send a value as a JSON text frame.

Source

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

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<()>

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) -> Self

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

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.