
use super::backend;
use crate::{Message, Result, traits::WebSocketSenderTrait};

/// Stream-based WebSocket sender.
#[derive(Debug)]
pub struct WebSocketSender<Backend: WebSocketSenderTrait = backend::WebSocketSender> {
    backend: Backend,
}

impl<Backend: WebSocketSenderTrait> WebSocketSender<Backend> {
    /// Sends a message.
    pub async fn send(&mut self, message: Message) -> Result<()> {
        self.backend.send(message).await
    }

    /// Closes the WebSocket.
    pub async fn close(&mut self, message: Option<(u16, String)>) -> Result<()> {
        self.backend.close(message).await
    }
}

impl<Backend: WebSocketSenderTrait> From<Backend> for WebSocketSender<Backend> {
    fn from(backend: Backend) -> Self {
        WebSocketSender { backend }
    }
}
