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>,
) -> Self
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.
Sourcepub fn identity(&self) -> String
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.
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>
pub fn parse_param<T>(&self, name: &str) -> Result<T>
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<()>
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.
Sourcepub fn send_json(&self, value: &impl Serialize) -> Result<()>
pub fn send_json(&self, value: &impl Serialize) -> Result<()>
Send a value as a JSON text frame.
Sourcepub fn close(&self) -> Result<()>
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.