pub struct RawClient { /* private fields */ }
Expand description

A socket which handles communication with the server. It’s initialized with a specific address as well as an optional namespace to connect to. If None is given the server will connect to the default namespace "/".

Implementations§

source§

impl RawClient

source

pub fn emit<E, D>(&self, event: E, data: D) -> Result<(), Error>where E: Into<Event>, D: Into<Payload>,

Sends a message to the server using the underlying engine.io protocol. This message takes an event, which could either be one of the common events like “message” or “error” or a custom event like “foo”. But be careful, the data string needs to be valid JSON. It’s recommended to use a library like serde_json to serialize the data properly.

Example
use rust_socketio::{ClientBuilder, RawClient, Payload};
use serde_json::json;

let mut socket = ClientBuilder::new("http://localhost:4200/")
    .on("test", |payload: Payload, socket: RawClient| {
        println!("Received: {:#?}", payload);
        socket.emit("test", json!({"hello": true})).expect("Server unreachable");
     })
    .connect()
    .expect("connection failed");

let json_payload = json!({"token": 123});

let result = socket.emit("foo", json_payload);

assert!(result.is_ok());
source

pub fn disconnect(&self) -> Result<(), Error>

Disconnects this client from the server by sending a socket.io closing packet.

Example
use rust_socketio::{ClientBuilder, Payload, RawClient};
use serde_json::json;

fn handle_test(payload: Payload, socket: RawClient) {
    println!("Received: {:#?}", payload);
    socket.emit("test", json!({"hello": true})).expect("Server unreachable");
}

let mut socket = ClientBuilder::new("http://localhost:4200/")
    .on("test", handle_test)
    .connect()
    .expect("connection failed");

let json_payload = json!({"token": 123});

socket.emit("foo", json_payload);

// disconnect from the server
socket.disconnect();
source

pub fn emit_with_ack<F, E, D>( &self, event: E, data: D, timeout: Duration, callback: F ) -> Result<(), Error>where F: FnMut(Payload, RawClient) + 'static + Send, E: Into<Event>, D: Into<Payload>,

Sends a message to the server but allocs an ack to check whether the server responded in a given time span. This message takes an event, which could either be one of the common events like “message” or “error” or a custom event like “foo”, as well as a data parameter. But be careful, in case you send a Payload::String, the string needs to be valid JSON. It’s even recommended to use a library like serde_json to serialize the data properly. It also requires a timeout Duration in which the client needs to answer. If the ack is acked in the correct time span, the specified callback is called. The callback consumes a Payload which represents the data send by the server.

Example
use rust_socketio::{ClientBuilder, Payload, RawClient};
use serde_json::json;
use std::time::Duration;
use std::thread::sleep;

let mut socket = ClientBuilder::new("http://localhost:4200/")
    .on("foo", |payload: Payload, _| println!("Received: {:#?}", payload))
    .connect()
    .expect("connection failed");

let ack_callback = |message: Payload, socket: RawClient| {
    match message {
        Payload::String(str) => println!("{}", str),
        Payload::Binary(bytes) => println!("Received bytes: {:#?}", bytes),
   }
};

let payload = json!({"token": 123});
socket.emit_with_ack("foo", payload, Duration::from_secs(2), ack_callback).unwrap();

sleep(Duration::from_secs(2));

Trait Implementations§

source§

impl Clone for RawClient

source§

fn clone(&self) -> RawClient

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

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

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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