use websock_proto::{ConnectOptions, Error, Result, WebSocketLimits};
use crate::Connection;
use crate::connection::connect;
#[derive(Debug, Clone)]
pub struct ClientBuilder {
opts: ConnectOptions,
}
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
}
impl ClientBuilder {
pub fn new() -> Self {
Self {
opts: ConnectOptions::default(),
}
}
pub fn with_options(mut self, opts: ConnectOptions) -> Self {
self.opts = opts;
self
}
pub fn options(&self) -> &ConnectOptions {
&self.opts
}
pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.opts.headers.push((name.into(), value.into()));
self
}
pub fn with_headers<I, K, V>(mut self, headers: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: Into<String>,
{
for (k, v) in headers {
self.opts.headers.push((k.into(), v.into()));
}
self
}
pub fn with_limits(mut self, limits: WebSocketLimits) -> Self {
self.opts.limits = limits;
self
}
pub fn with_protocol(mut self, protocol: impl Into<String>) -> Self {
self.opts.protocols.push(protocol.into());
self
}
pub fn with_protocols<I, P>(mut self, protocols: I) -> Self
where
I: IntoIterator<Item = P>,
P: Into<String>,
{
for p in protocols {
self.opts.protocols.push(p.into());
}
self
}
pub fn build(self) -> Client {
Client { opts: self.opts }
}
pub fn with_system_roots(self) -> Result<Client> {
Ok(self.build())
}
pub fn with_server_certificates<I>(self, _chain: I) -> Result<Client>
where
I: IntoIterator<Item = Vec<u8>>,
{
Err(Error::Unsupported(
"custom certificates are not supported in browser wasm".into(),
))
}
pub fn dangerous(self) -> DangerousClientBuilder {
DangerousClientBuilder { opts: self.opts }
}
}
#[derive(Debug, Clone)]
pub struct Client {
opts: ConnectOptions,
}
impl Client {
pub fn options(&self) -> &ConnectOptions {
&self.opts
}
pub async fn connect(&self, url: &str) -> Result<Connection> {
connect(url, self.opts.clone()).await
}
}
pub struct DangerousClientBuilder {
#[allow(dead_code)]
opts: ConnectOptions,
}
impl DangerousClientBuilder {
pub fn with_no_certificate_verification(self) -> Result<Client> {
Err(Error::Unsupported(
"certificate verification cannot be disabled in browser wasm".into(),
))
}
}