Skip to main content

Socket

Struct Socket 

Source
pub struct Socket<D>
where D: Default + Send + Sync + 'static,
{ 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 :

  • the packet communication between with the Engine and the user defined Handler.
  • the user defined Data bound to the socket.
  • the heartbeat job that verify that the connection is still up by sending packets periodically.

Fields§

§id: Sid

The socket id

§protocol: ProtocolVersion

The protocol version used by the socket

§data: D

User data bound to the socket

§req_parts: Parts

Http Request data used to create a socket

Implementations§

Source§

impl<D> Socket<D>
where D: Default + Send + Sync + 'static,

Source

pub fn transport_type(&self) -> TransportType

Returns the current TransportType of the Socket

Source

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.

Source

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

Source

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.

Source

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

Source

pub async fn closed(&self)

Wait for the socket to be fully closed

Source

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

Source

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

Source

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.

Source

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.

Trait Implementations§

Source§

impl<D: Default + Send + Sync + 'static> Debug for Socket<D>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<D> !Freeze for Socket<D>

§

impl<D> !RefUnwindSafe for Socket<D>

§

impl<D> !UnwindSafe for Socket<D>

§

impl<D> Send for Socket<D>

§

impl<D> Sync for Socket<D>

§

impl<D> Unpin for Socket<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for Socket<D>
where D: UnsafeUnpin,

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

Source§

type Output = T

Should always be Self
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