Struct my_rust_socketio::client::ClientBuilder
source · pub struct ClientBuilder { /* private fields */ }
Expand description
A builder class for a socket.io
socket. This handles setting up the client and
configuring the callback, the namespace and metadata of the socket. If no
namespace is specified, the default namespace /
is taken. The connect
method
acts the build
method and returns a connected Client
.
Implementations§
source§impl ClientBuilder
impl ClientBuilder
sourcepub fn new<T: Into<String>>(address: T) -> Self
pub fn new<T: Into<String>>(address: T) -> Self
Create as client builder from a URL. URLs must be in the form
[ws or wss or http or https]://[domain]:[port]/[path]
. The
path of the URL is optional and if no port is given, port 80
will be used.
Example
use rust_socketio::{ClientBuilder, Payload, RawClient};
use serde_json::json;
let callback = |payload: Payload, socket: RawClient| {
match payload {
Payload::String(str) => println!("Received: {}", str),
Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data),
}
};
let mut socket = ClientBuilder::new("http://localhost:4200")
.namespace("/admin")
.on("test", callback)
.connect()
.expect("error while connecting");
// use the socket
let json_payload = json!({"token": 123});
let result = socket.emit("foo", json_payload);
assert!(result.is_ok());
sourcepub fn namespace<T: Into<String>>(self, namespace: T) -> Self
pub fn namespace<T: Into<String>>(self, namespace: T) -> Self
Sets the target namespace of the client. The namespace should start
with a leading /
. Valid examples are e.g. /admin
, /foo
.
pub fn reconnect(self, reconnect: bool) -> Self
sourcepub fn reconnect_on_disconnect(self, reconnect_on_disconnect: bool) -> Self
pub fn reconnect_on_disconnect(self, reconnect_on_disconnect: bool) -> Self
If set to true
automatically set try to reconnect when the server
disconnects the client.
Defaults to false
.
Example
use rust_socketio::ClientBuilder;
let socket = ClientBuilder::new("http://localhost:4200/")
.reconnect_on_disconnect(true)
.connect();
pub fn reconnect_delay(self, min: u64, max: u64) -> Self
pub fn max_reconnect_attempts(self, reconnect_attempts: u8) -> Self
sourcepub fn on<T: Into<Event>, F>(self, event: T, callback: F) -> Selfwhere
F: FnMut(Payload, RawClient) + 'static + Send,
pub fn on<T: Into<Event>, F>(self, event: T, callback: F) -> Selfwhere F: FnMut(Payload, RawClient) + 'static + Send,
Registers a new callback for a certain crate::event::Event
. The event could either be
one of the common events like message
, error
, open
, close
or a custom
event defined by a string, e.g. onPayment
or foo
.
Example
use rust_socketio::{ClientBuilder, Payload};
let socket = ClientBuilder::new("http://localhost:4200/")
.namespace("/admin")
.on("test", |payload: Payload, _| {
match payload {
Payload::String(str) => println!("Received: {}", str),
Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data),
}
})
.on("error", |err, _| eprintln!("Error: {:#?}", err))
.connect();
sourcepub fn on_any<F>(self, callback: F) -> Selfwhere
F: FnMut(Event, Payload, RawClient) + 'static + Send,
pub fn on_any<F>(self, callback: F) -> Selfwhere F: FnMut(Event, Payload, RawClient) + 'static + Send,
Registers a Callback for all crate::event::Event::Custom
and crate::event::Event::Message
.
Example
use rust_socketio::{ClientBuilder, Payload};
let client = ClientBuilder::new("http://localhost:4200/")
.namespace("/admin")
.on_any(|event, payload, _client| {
if let Payload::String(str) = payload {
println!("{} {}", String::from(event), str);
}
})
.connect();
sourcepub fn tls_config(self, tls_config: TlsConnector) -> Self
pub fn tls_config(self, tls_config: TlsConnector) -> Self
Uses a preconfigured TLS connector for secure communication. This configures
both the polling
as well as the websocket
transport type.
Example
use rust_socketio::{ClientBuilder, Payload};
use native_tls::TlsConnector;
let tls_connector = TlsConnector::builder()
.use_sni(true)
.build()
.expect("Found illegal configuration");
let socket = ClientBuilder::new("http://localhost:4200/")
.namespace("/admin")
.on("error", |err, _| eprintln!("Error: {:#?}", err))
.tls_config(tls_connector)
.connect();
sourcepub fn opening_header<T: Into<HeaderValue>, K: Into<String>>(
self,
key: K,
val: T
) -> Self
pub fn opening_header<T: Into<HeaderValue>, K: Into<String>>( self, key: K, val: T ) -> Self
Sets custom http headers for the opening request. The headers will be passed to the underlying transport type (either websockets or polling) and then get passed with every request thats made. via the transport layer.
Example
use rust_socketio::{ClientBuilder, Payload};
let socket = ClientBuilder::new("http://localhost:4200/")
.namespace("/admin")
.on("error", |err, _| eprintln!("Error: {:#?}", err))
.opening_header("accept-encoding", "application/json")
.connect();
sourcepub fn auth(self, auth: Value) -> Self
pub fn auth(self, auth: Value) -> Self
Sets data sent in the opening request.
Example
use rust_socketio::{ClientBuilder};
use serde_json::json;
let socket = ClientBuilder::new("http://localhost:4204/")
.namespace("/admin")
.auth(json!({ "password": "1337" }))
.on("error", |err, _| eprintln!("Error: {:#?}", err))
.connect()
.expect("Connection error");
sourcepub fn transport_type(self, transport_type: TransportType) -> Self
pub fn transport_type(self, transport_type: TransportType) -> Self
Specifies which EngineIO TransportType
to use.
Example
use rust_socketio::{ClientBuilder, TransportType};
use serde_json::json;
let socket = ClientBuilder::new("http://localhost:4200/")
// Use websockets to handshake and connect.
.transport_type(TransportType::Websocket)
.connect()
.expect("connection failed");
// use the socket
let json_payload = json!({"token": 123});
let result = socket.emit("foo", json_payload);
assert!(result.is_ok());
sourcepub fn connect(self) -> Result<Client, Error>
pub fn connect(self) -> Result<Client, Error>
Connects the socket to a certain endpoint. This returns a connected
Client
instance. This method returns an std::result::Result::Err
value if something goes wrong during connection. Also starts a separate
thread to start polling for packets. Used with callbacks.
Example
use rust_socketio::{ClientBuilder, Payload};
use serde_json::json;
let mut socket = ClientBuilder::new("http://localhost:4200/")
.namespace("/admin")
.on("error", |err, _| eprintln!("Client error!: {:#?}", err))
.connect()
.expect("connection failed");
// use the socket
let json_payload = json!({"token": 123});
let result = socket.emit("foo", json_payload);
assert!(result.is_ok());
pub fn connect_raw(self) -> Result<RawClient, Error>
Trait Implementations§
source§impl Clone for ClientBuilder
impl Clone for ClientBuilder
source§fn clone(&self) -> ClientBuilder
fn clone(&self) -> ClientBuilder
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more