pub struct WebSocket { /* private fields */ }
Expand description
Represents a WebSocket instance.
This struct manages broadcast capabilities and holds the internal broadcast map responsible for handling message distribution to various WebSocket connections.
Implementations§
Source§impl WebSocket
impl WebSocket
Sourcepub fn new() -> WebSocket
pub fn new() -> WebSocket
Creates a new WebSocket instance.
Initializes with a default broadcast map.
§Returns
WebSocket
- A new WebSocket instance.
Sourcepub fn receiver_count<'a, B>(&self, broadcast_type: BroadcastType<B>) -> usizewhere
B: BroadcastTypeTrait,
pub fn receiver_count<'a, B>(&self, broadcast_type: BroadcastType<B>) -> usizewhere
B: BroadcastTypeTrait,
Retrieves the current receiver count for a given broadcast type.
§Type Parameters
B
: The type implementingBroadcastTypeTrait
.
§Arguments
BroadcastType<B>
- The broadcast type for which to get the receiver count.
§Returns
ReceiverCount
- The number of active receivers for the broadcast type, or 0 if not found.
Sourcepub fn receiver_count_after_increment<B>(
&self,
broadcast_type: BroadcastType<B>,
) -> usizewhere
B: BroadcastTypeTrait,
pub fn receiver_count_after_increment<B>(
&self,
broadcast_type: BroadcastType<B>,
) -> usizewhere
B: BroadcastTypeTrait,
Calculates the receiver count after incrementing it.
Ensures the count does not exceed the maximum allowed value minus one.
§Type Parameters
B
: The type implementingBroadcastTypeTrait
.
§Arguments
BroadcastType<B>
- The broadcast type for which to increment the receiver count.
§Returns
ReceiverCount
- The incremented receiver count.
Sourcepub fn receiver_count_after_decrement<B>(
&self,
broadcast_type: BroadcastType<B>,
) -> usizewhere
B: BroadcastTypeTrait,
pub fn receiver_count_after_decrement<B>(
&self,
broadcast_type: BroadcastType<B>,
) -> usizewhere
B: BroadcastTypeTrait,
Calculates the receiver count after decrementing it.
Ensures the count does not go below 0.
§Type Parameters
B
: The type implementingBroadcastTypeTrait
.
§Arguments
BroadcastType<B>
- The broadcast type for which to decrement the receiver count.
§Returns
ReceiverCount
- The decremented receiver count.
Sourcepub fn send<T, B>(
&self,
broadcast_type: BroadcastType<B>,
data: T,
) -> Result<Option<usize>, SendError<Vec<u8>>>
pub fn send<T, B>( &self, broadcast_type: BroadcastType<B>, data: T, ) -> Result<Option<usize>, SendError<Vec<u8>>>
Sends data to all active receivers for a given broadcast type.
§Type Parameters
T
: The type of data to send, which must be convertible toVec<u8>
.B
: The type implementingBroadcastTypeTrait
.
§Arguments
BroadcastType<B>
- The broadcast type to which to send the data.T
- The data to send.
§Returns
BroadcastMapSendResult<Vec<u8>>
- A result indicating the success or failure of the send operation.
Sourcepub async fn run<B>(&self, config: WebSocketConfig<B>)where
B: BroadcastTypeTrait,
pub async fn run<B>(&self, config: WebSocketConfig<B>)where
B: BroadcastTypeTrait,
Runs the WebSocket connection, handling incoming requests and outgoing messages.
This asynchronous function continuously monitors for new WebSocket requests and incoming broadcast messages, processing them according to the configured hooks.
§Type Parameters
B
: The type implementingBroadcastTypeTrait
.
§Arguments
WebSocketConfig<B>
- The WebSocket configuration containing the configuration for this WebSocket instance.
§Panics
Panics if the context in the WebSocket configuration is not set (i.e., it’s the default context).
Panics if the broadcast type in the WebSocket configuration is BroadcastType::Unknown
.