I'm using tungstenite
(along with native-tls
feature for its TlsStream
) + mio
(for Poll
) in order to implement a client program to connect to target secure websocket (wss).
I've come across the very good and very close to what I want as seen here. That code works with non-secure websocket connection. But will fail with the HTTP error code 400 i.e. Bad request if I swap in wss url instead. So that means I'm on the right path, just that the stream needs to supports Tls.
My objective is to use Poll
in order to be able to receive incoming messages from websocket, and be able to push some message to it within the main message loop.
With some research, I modified the initial set up code to be as follows
fn main() {
// Create a poll instance.
let mut poll = Poll::new().unwrap();
const STREAM_TOKEN: Token = Token(10);
const WAKE_TOKEN: Token = Token(20);
// Create storage for events.
let mut events = Events::with_capacity(128);
let std_stream = StdStream::connect("target-hostname.com:443").unwrap();
// for tls
let stream = TlsConnector::new().unwrap().connect("wss://target-hostname.com/topic", std_stream).unwrap();
let hs = ClientHandshake::start(
stream,
String::from("wss://target-hostname.com/topic")
.into_client_request()
.unwrap(),
None,
)
.unwrap();
...
// Register the socket with `Poll`
poll.registry()
.register(ws.get_mut(), STREAM_TOKEN, Interest::READABLE) // <------- problem
.unwrap();
...
As commented in the code,
native_tls::TlsStream
doesn't implement mio::event::Source
trait so it is not compatible to be used with Poll::registry::regiter
function as seen in the code. It shows the following error
error[E0277]: the trait bound `TlsStream<std::net::TcpStream>: Source` is not satisfied
--> src/main.rs:53:19
|
53 | .register(ws.get_mut(), STREAM_TOKEN, Interest::READABLE)
| -------- ^^^^^^^^^^^^ the trait `Source` is not implemented for `TlsStream<std::net::TcpStream>`
| |
| required by a bound introduced by this call
|
note: required by a bound in `Registry::register`
--> /home/something/.cargo/registry/src/github.com-1ecc6299db9ec823/mio-0.8.0/src/poll.rs:499:12
|
499 | S: event::Source + ?Sized,
| ^^^^^^^^^^^^^ required by this bound in `Registry::register`
I see mio::net::TcpStream
on how it implements mio::event::Source
here. But i'm not sure how to proceed on this as the underlying IoSource
would need to handle things across the board from start.
So, my question is
How can I make tungstenite
with native-tls
feature works with mio
(for its Poll
feature)?