Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I store the result of tokio_tungstenite::connect::connect_async() as a field of a struct?

I’m trying to build a struct that handles async websocket communication. I’m working on getting the connection set up and, as part of that, I’m using tokio_tungstenite::connect::connect_async(). I want to store its returned value as a field in the struct I’m working on.

I’ve tried the following:

use tokio_tungstenite::tungstenite::stream::MaybeTlsStream;
use tokio_tungstenite::WebSocketStream;
use tokio_tungstenite::connect_async;
use crate::WrAuth;
use crate::net::wr_ip_addr::WrIPAddress;

pub struct WrAsyncSocket {
    m_auth: WrAuth,
    m_ip_address: WrIPAddress,
    m_socket_stream: Option<WebSocketStream<MaybeTlsStream<TcpStream>>>,
    m_require_all: bool,
}

impl WrAsyncSocket {

    pub fn new(x_auth: WrAuth, x_ip_address: WrIPAddress, x_socket_stream: Option<WebSocketStream<MaybeTlsStream<TcpStream>>>, x_require_all: bool) -> Self {
        Self {
            m_auth: x_auth,
            m_ip_address: x_ip_address,
            m_socket_stream: x_socket_stream,
            m_require_all: x_require_all,
        }
    }

    pub async fn connect_async(&mut self) {
        let (ws_stream, _) = connect_async(self.m_ip_address.clone().get_url()).await.expect("Failed to connect.");
        self.m_socket_stream = Some(ws_stream);
    }
}

With this, I’ve tried importing both

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

tokio::net::tcp::stream::TcpStream

and

tokio::net::TcpStream

however, both result in the following error on any instance of TcpStream:

the trait bound `tokio::net::TcpStream: std::io::Read` is not satisfied
the trait `std::io::Read` is not implemented for `tokio::net::TcpStream`rustcClick for full compiler diagnostic
stream.rs(63, 28): required by a bound in `tokio_tungstenite::tungstenite::stream::MaybeTlsStream`

Any advice on what I should be doing here? As a side note: I’m new to Rust and tokio_tungstenite so help/tips in any regard would be greatly appreciated. Be as critical as you’d like.

>Solution :

You likely want to import tokio_tungstenite::MaybeTlsStream instead. The one you are using is a re-export from the tungstenite crate (which is not async) while this is the async version of it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading