pub struct Socketeer<RxMessage, TxMessage, const CHANNEL_SIZE: usize = 4> { /* private fields */ }Expand description
A WebSocket client that manages the connection to a WebSocket server. The client can send and receive messages, and will transparently handle protocol messages.
§Type Parameters
RxMessage: The type of message that the client will receive from the server.TxMessage: The type of message that the client will send to the server.CHANNEL_SIZE: The size of the internal channels used to communicate between the task managing the WebSocket connection and the client.
Implementations§
Source§impl<RxMessage: for<'a> Deserialize<'a> + Debug, TxMessage: Serialize + Debug, const CHANNEL_SIZE: usize> Socketeer<RxMessage, TxMessage, CHANNEL_SIZE>
impl<RxMessage: for<'a> Deserialize<'a> + Debug, TxMessage: Serialize + Debug, const CHANNEL_SIZE: usize> Socketeer<RxMessage, TxMessage, CHANNEL_SIZE>
Sourcepub async fn connect(
url: &str,
) -> Result<Socketeer<RxMessage, TxMessage, CHANNEL_SIZE>, Error>
pub async fn connect( url: &str, ) -> Result<Socketeer<RxMessage, TxMessage, CHANNEL_SIZE>, Error>
Create a Socketeer connected to the provided URL.
Once connected, Socketeer manages the underlying WebSocket connection, transparently handling protocol messages.
§Errors
- If the URL cannot be parsed
- If the WebSocket connection to the requested URL fails
Sourcepub async fn next_message(&mut self) -> Result<RxMessage, Error>
pub async fn next_message(&mut self) -> Result<RxMessage, Error>
Wait for the next parsed message from the WebSocket connection.
§Errors
- If the WebSocket connection is closed or otherwise errored
- If the message cannot be deserialized
Sourcepub async fn send(&self, message: TxMessage) -> Result<(), Error>
pub async fn send(&self, message: TxMessage) -> Result<(), Error>
Send a message to the WebSocket connection. This function will wait for the message to be sent before returning.
§Errors
- If the message cannot be serialized
- If the WebSocket connection is closed, or otherwise errored
Sourcepub async fn reconnect(self) -> Result<Self, Error>
pub async fn reconnect(self) -> Result<Self, Error>
Consume self, closing down any remaining send/recieve, and return a new Socketeer instance if successful This function attempts to close the connection gracefully before returning, but will not return an error if the connection is already closed, as its intended use is to re-establish a failed connection.
§Errors
- If a new connection cannot be established