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() -> Self
 
pub fn new() -> Self
Creates a new WebSocket instance.
Initializes with a default broadcast map.
§Returns
- WebSocket- A new WebSocket instance.
Sourcepub fn receiver_count<B: BroadcastTypeTrait>(
    &self,
    broadcast_type: BroadcastType<B>,
) -> ReceiverCount
 
pub fn receiver_count<B: BroadcastTypeTrait>( &self, broadcast_type: BroadcastType<B>, ) -> ReceiverCount
Retrieves the current receiver count for a given broadcast type.
§Type Parameters
- B: The type implementing- BroadcastTypeTrait.
§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: BroadcastTypeTrait>(
    &self,
    broadcast_type: BroadcastType<B>,
) -> ReceiverCount
 
pub fn receiver_count_after_increment<B: BroadcastTypeTrait>( &self, broadcast_type: BroadcastType<B>, ) -> ReceiverCount
Calculates the receiver count after incrementing it.
Ensures the count does not exceed the maximum allowed value minus one.
§Type Parameters
- B: The type implementing- BroadcastTypeTrait.
§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: BroadcastTypeTrait>(
    &self,
    broadcast_type: BroadcastType<B>,
) -> ReceiverCount
 
pub fn receiver_count_after_decrement<B: BroadcastTypeTrait>( &self, broadcast_type: BroadcastType<B>, ) -> ReceiverCount
Calculates the receiver count after decrementing it.
Ensures the count does not go below 0.
§Type Parameters
- B: The type implementing- BroadcastTypeTrait.
§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,
) -> BroadcastMapSendResult<Vec<u8>>
 
pub fn send<T, B>( &self, broadcast_type: BroadcastType<B>, data: T, ) -> BroadcastMapSendResult<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 to- Vec<u8>.
- B: The type implementing- BroadcastTypeTrait.
§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: BroadcastTypeTrait>(&self, config: WebSocketConfig<B>)
 
pub async fn run<B: BroadcastTypeTrait>(&self, config: WebSocketConfig<B>)
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 implementing- BroadcastTypeTrait.
§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.