tungstenite-rs
SixoneKui opened this issue · 2 comments
Hi, I am moving from js to rust.
With the JS wss module, there is a way to set up a lookup function in websocket connection and thereby, with any domain, I can specify the IP ( the usecase is cloudflare , which matches multiple IPs to a domain but I want only connect one ).
Can I do this with tungstenite-rs?
Thank you very much.
Hi,
yes and no. Yes, you can do this with Tungstenite. No, Tungstenite won't just do it for you magically. The connect()
function in Tungstenite is provided for convenience and is not intended to handle complex use-cases. What you want is the client_tls()
function (or just client()
if you prefer to handle TLS on your own). With this function you make the ordinary TCP connection first by calling TcpStream::connect()
from the standard library. At this time you are free to decide how to connect. After the connection is ready, you pass it directly to Tungstenite which upgrades it to WebSocket and handles the rest for you.
As a side note, I suggest looking at the tokio-tungstenite
crate. While tungstenite
handles most simple use cases, it is single-thread, single-task if just used alone. Most users don't use Tungstenite directly. There are wrappers around it for use in higher-level multi-task environments: tokio-tungstenite
for those who uses Tokio, async-tungstenite
for those who prefer to go with async-std
, and many smaller ones for less generic uses. Depending on what you want to write, I suggest taking one of them. However, if your task is as simple as making one and only WebSocket connection once and do some simple and short data transfer in something like a command-line application, then direct Tungstenite usage is the right one.
thank you very much.
It works well.