Gelbpunkt / tokio-websockets

High performance, strict, tokio-util based websockets implementation
MIT License
57 stars 11 forks source link

FR: Allow access to the underlying stream from WebSocketStream #71

Open devanlai opened 2 days ago

devanlai commented 2 days ago

Currently there's not a convenient way to get a shared reference to the underlying stream from a WebSocketStream<S>. I could use Builder::connect_on() to connect to an established stream that I've already prepared, but as far as I can tell, there's not a clean way to let connect() establish the stream for me and get a reference to it.

In my use case, I'd like to be able to inspect the underlying MaybeTlsStream<T> that's internally inside WebSocketStream<S>. That way I can inspect the peer certificates and other TLS connection properties to verify certain security properties and log them.

tokio-tungstenite provides a WebSocketStream::get_ref() method that provides a shared reference to the underlying stream.

It also provides a get_mut() method to get a mutable reference, but that seems a little dangerous.

Gelbpunkt commented 2 days ago

I wanted to add get_mut() anyways seeing how more advanced usecases like setting socket options post-connect are enabled by it. It will definitely need a note that writing and reading from the I/O should never be done.