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
impl Socket
Sourcepub fn new(
id: SocketId,
path: impl Into<Arc<str>>,
params: Vec<(String, String)>,
outbound: UnboundedSender<Outbound>,
) -> Socket
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.
Sourcepub fn param(&self, name: &str) -> Option<&str>
pub fn param(&self, name: &str) -> Option<&str>
A route parameter captured from the path — rooms/{room} gives
room.
Sourcepub fn parse_param<T>(&self, name: &str) -> Result<T, Error>
pub fn parse_param<T>(&self, name: &str) -> Result<T, Error>
A route parameter, parsed.
A bad request when it will not parse: the value came from the URL the client asked for.
Sourcepub fn send(&self, message: impl Into<Message>) -> Result<(), Error>
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.
Sourcepub fn send_json(&self, value: &impl Serialize) -> Result<(), Error>
pub fn send_json(&self, value: &impl Serialize) -> Result<(), Error>
Send a value as a JSON text frame.
Sourcepub fn close(&self) -> Result<(), Error>
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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Socket
impl RefUnwindSafe for Socket
impl Send for Socket
impl Sync for Socket
impl Unpin for Socket
impl UnsafeUnpin for Socket
impl UnwindSafe for Socket
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Event for T
impl<T> Event for T
Source§fn event_name() -> &'static strwhere
Self: Sized,
fn event_name() -> &'static strwhere
Self: Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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