pub struct Socket<D>{
pub id: Sid,
pub protocol: ProtocolVersion,
pub data: D,
pub req_parts: Parts,
/* private fields */
}Expand description
A Socket represents a client connection to the server.
It is agnostic to the TransportType.
It handles :
Fields§
§id: SidThe socket id
protocol: ProtocolVersionThe protocol version used by the socket
data: DUser data bound to the socket
req_parts: PartsHttp Request data used to create a socket
Implementations§
Source§impl<D> Socket<D>
impl<D> Socket<D>
Sourcepub fn transport_type(&self) -> TransportType
pub fn transport_type(&self) -> TransportType
Returns the current TransportType of the Socket
Sourcepub fn reserve(&self) -> Result<Permit<'_>, TrySendError<()>>
pub fn reserve(&self) -> Result<Permit<'_>, TrySendError<()>>
Reserve n permits to emit multiple messages and ensure that there is enough
space in the internal chan.
If the internal chan is full, the function will return a TrySendError::Full error.
If the socket is closed, the function will return a TrySendError::Closed error.
Sourcepub fn emit(&self, msg: impl Into<Str>) -> Result<(), TrySendError<Str>>
pub fn emit(&self, msg: impl Into<Str>) -> Result<(), TrySendError<Str>>
Emits a message to the client.
If the transport is in websocket mode, the message is directly sent as a text frame.
If the transport is in polling mode, the message is buffered and sent as a text frame to the next polling request.
⚠️ If the buffer is full or the socket is disconnected, an error will be returned with the original data
Sourcepub fn close(&self, reason: DisconnectReason)
pub fn close(&self, reason: DisconnectReason)
Immediately closes the socket and the underlying connection.
The socket will be removed from the Engine and the Handler will be notified.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns true if the socket is closed It means that no more packets can be sent to the client
Sourcepub fn emit_binary<B: Into<Bytes>>(
&self,
data: B,
) -> Result<(), TrySendError<Bytes>>
pub fn emit_binary<B: Into<Bytes>>( &self, data: B, ) -> Result<(), TrySendError<Bytes>>
Emits a binary message to the client.
If the transport is in websocket mode, the message is directly sent as a binary frame.
If the transport is in polling mode, the message is buffered and sent as a text frame encoded in base64 to the next polling request.
⚠️ If the buffer is full or the socket is disconnected, an error will be returned with the original data
Sourcepub fn emit_volatile(&self, msg: impl Into<Str>) -> bool
pub fn emit_volatile(&self, msg: impl Into<Str>) -> bool
Try to send a volatile message bypassing the internal buffer channel. Volatile messages may be dropped if the transport is not ready to receive them.
Because volatile messages bypass the main mpsc buffer queue, they may arrive out of order relative to regular messages.
Returns true if the message was queued for sending, false if it
was dropped (channel full or transport shutting down).
Sourcepub fn emit_binary_volatile<B: Into<Bytes>>(&self, data: B) -> bool
pub fn emit_binary_volatile<B: Into<Bytes>>(&self, data: B) -> bool
Try to send a volatile binary message bypassing the internal buffer channel. Volatile messages may be dropped if the transport is not ready to receive them.
Returns true if the message was queued for sending, false if it
was dropped.
Sourcepub fn emit_many_volatile(&self, msg: Str, data: VecDeque<Bytes>) -> bool
pub fn emit_many_volatile(&self, msg: Str, data: VecDeque<Bytes>) -> bool
Try to send a volatile message with multiple adjacent binary payloads. The message and all binary payloads are sent atomically as a single volatile write.
Returns true if the message was queued for sending, false if it
was dropped.