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

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, Client};
use serde_json::json;


let callback = |payload: Payload, socket: Client| {
           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());

Sets the target namespace of the client. The namespace should start with a leading /. Valid examples are e.g. /admin, /foo.

Registers a new callback for a certain [socketio::event::Event]. The event could either be one of the common events like message, error, connect, 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();

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();

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();

Specifies which EngineIO TransportType to use.

Example
use rust_socketio::{ClientBuilder, TransportType};

let socket = ClientBuilder::new("http://localhost:4200/")
    // Use websockets to handshake and connect.
    .transport_type(TransportType::Websocket)
    .connect()
    .expect("connection failed");

// use the socket

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());

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Get the TypeId of this object.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more