1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::message::{Message, WSPayload};
use crate::messages::Messages;
use crate::{TcpStream, WsError};
use futures_lite::AsyncWriteExt;

#[derive(Debug)]
pub struct TcpStreamNT {
    tcp_stream: TcpStream,
}
impl TcpStreamNT {
    pub async fn send<'a, I>(&mut self, data: I) -> Result<(), WsError>
    where
        //I: Into<&'a [u8]> + 'a,
        I: Into<WSPayload>,
    {
        self.tcp_stream
            .write_all(I::into(data).as_ref())
            .await
            .map_err(|_| WsError::Unknown)
    }
    pub async fn close(&mut self) {
        let _ = self.send(Message::Close).await;
    }
}
pub struct WsConnection {
    pub(crate) tcp_stream: TcpStream,
}
impl WsConnection {
    pub fn new(tcp_stream: TcpStream) -> WsConnection {
        WsConnection { tcp_stream }
    }
    pub fn messages(&mut self) -> Messages {
        Messages::new(self)
    }
    pub fn get_connection(&self) -> TcpStreamNT {
        TcpStreamNT {
            tcp_stream: self.tcp_stream.clone(),
        }
    }

    pub async fn send<'a, I>(&mut self, data: I) -> Result<(), WsError>
    where
        //I: Into<&'a [u8]> + 'a,
        I: Into<WSPayload>,
    {
        self.tcp_stream
            .write_all(I::into(data).as_ref())
            .await
            .map_err(|_| WsError::Unknown)
    }
}