Struct rust_socketio::SocketBuilder[][src]

pub struct SocketBuilder { /* fields omitted */ }
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 Socket.

Implementations

impl SocketBuilder[src]

pub fn new<T: Into<String>>(address: T) -> Self[src]

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::{SocketBuilder, Payload};
use serde_json::json;


let callback = |payload: Payload, _| {
           match payload {
               Payload::String(str) => println!("Received: {}", str),
               Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data),
           }
};

let mut socket = SocketBuilder::new("http://localhost:4200")
    .set_namespace("/admin")
    .expect("illegal namespace")
    .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());

pub fn set_namespace<T: Into<String>>(self, namespace: T) -> Result<Self, Error>[src]

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

pub fn on<F>(self, event: &str, callback: F) -> Self where
    F: FnMut(Payload, Socket) + 'static + Sync + Send
[src]

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::{SocketBuilder, Payload};

let callback = |payload: Payload, _| {
           match payload {
               Payload::String(str) => println!("Received: {}", str),
               Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data),
           }
};

let socket = SocketBuilder::new("http://localhost:4200")
    .set_namespace("/admin")
    .expect("illegal namespace")
    .on("test", callback)
    .on("error", |err, _| eprintln!("Error: {:#?}", err))
    .connect();

pub fn set_tls_config(self, tls_config: TlsConnector) -> Self[src]

Uses a preconfigured TLS connector for secure cummunication. This configures both the polling as well as the websocket transport type.

Example

use rust_socketio::{SocketBuilder, Payload};
use native_tls::TlsConnector;

let tls_connector =  TlsConnector::builder()
           .use_sni(true)
           .build()
           .expect("Found illegal configuration");

let socket = SocketBuilder::new("http://localhost:4200")
    .set_namespace("/admin")
    .expect("illegal namespace")
    .on("error", |err, _| eprintln!("Error: {:#?}", err))
    .set_tls_config(tls_connector)
    .connect();

pub fn set_opening_header<K: IntoHeaderName>(
    self,
    key: K,
    val: HeaderValue
) -> Self
[src]

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::{SocketBuilder, Payload};
use reqwest::header::{ACCEPT_ENCODING};


let socket = SocketBuilder::new("http://localhost:4200")
    .set_namespace("/admin")
    .expect("illegal namespace")
    .on("error", |err, _| eprintln!("Error: {:#?}", err))
    .set_opening_header(ACCEPT_ENCODING, "application/json".parse().unwrap())
    .connect();

pub fn connect(self) -> Result<Socket, Error>[src]

Connects the socket to a certain endpoint. This returns a connected Socket instance. This method returns an std::result::Result::Err value if something goes wrong during connection.

Example

use rust_socketio::{SocketBuilder, Payload};
use serde_json::json;


let mut socket = SocketBuilder::new("http://localhost:4200")
    .set_namespace("/admin")
    .expect("illegal namespace")
    .on("error", |err, _| eprintln!("Socket 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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

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

fn in_current_span(self) -> Instrumented<Self>[src]

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<T> Typeable for T where
    T: Any

fn get_type(&self) -> TypeId

Get the TypeId of this object.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V