Struct tokio_websockets::client::Builder
source · pub struct Builder<'a, R: Resolver = Gai> { /* private fields */ }
client
only.Expand description
Builder for WebSocket client connections.
Implementations§
source§impl<'a, R: Resolver> Builder<'a, R>
impl<'a, R: Resolver> Builder<'a, R>
sourcepub fn uri(self, uri: &str) -> Result<Self, InvalidUri>
pub fn uri(self, uri: &str) -> Result<Self, InvalidUri>
Sets the Uri
to connect to. This URI must use the ws
or wss
schemes.
§Errors
This method returns a http::uri::InvalidUri
error if URI parsing
fails.
sourcepub fn connector(self, connector: &'a Connector) -> Self
pub fn connector(self, connector: &'a Connector) -> Self
Sets the TLS connector for the client.
By default, the client will create a new one for each connection instead of reusing one.
sourcepub fn resolver<NewR: Resolver>(self, resolver: NewR) -> Builder<'a, NewR>
pub fn resolver<NewR: Resolver>(self, resolver: NewR) -> Builder<'a, NewR>
Sets the DNS resolver for the client.
By default, the client will use the Gai
resolver, a wrapper around
the blocking getaddrinfo
syscall.
sourcepub fn add_header(self, name: HeaderName, value: HeaderValue) -> Self
pub fn add_header(self, name: HeaderName, value: HeaderValue) -> Self
Adds an extra HTTP header to the handshake request.
sourcepub async fn connect(
&self,
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), Error>
pub async fn connect( &self, ) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), Error>
Establishes a connection to the WebSocket server. This requires a URI to
be configured via Builder::uri
.
§Errors
This method returns an Error
if connecting to the server fails or no
URI has been configured.
sourcepub async fn connect_on<S: AsyncRead + AsyncWrite + Unpin>(
&self,
stream: S,
) -> Result<(WebSocketStream<S>, Response), Error>
pub async fn connect_on<S: AsyncRead + AsyncWrite + Unpin>( &self, stream: S, ) -> Result<(WebSocketStream<S>, Response), Error>
Takes over an already established stream and uses it to send and receive
WebSocket messages. This requires a URI to be configured via
Builder::uri
.
This method assumes that the TLS connection has already been established, if needed. It sends an HTTP upgrade request and waits for an HTTP Switching Protocols response before proceeding.
§Errors
This method returns an Error
if writing or reading from the stream
fails or no URI has been configured.
sourcepub fn take_over<S: AsyncRead + AsyncWrite + Unpin>(
&self,
stream: S,
) -> WebSocketStream<S>
pub fn take_over<S: AsyncRead + AsyncWrite + Unpin>( &self, stream: S, ) -> WebSocketStream<S>
Takes over an already established stream that has already performed a HTTP upgrade handshake and uses it to send and receive WebSocket messages.
This method will not perform a TLS handshake or a HTTP upgrade handshake, it assumes the stream is ready to use for writing and reading the WebSocket protocol.