tf-rust-socketio 0.7.0

A Socket.IO client implementation in Rust. Fork of rust_socketio with server-to-client ACK support, reconnect headers, and close reason enhancements.
Documentation
use serde_json::json;
use tf_rust_socketio::{ClientBuilder, Event, Payload, RawClient};

fn handle_foo(payload: Payload, socket: RawClient) -> () {
    socket.emit("bar", payload).expect("Server unreachable")
}

fn main() {
    // define a callback which is called when a payload is received
    // this callback gets the payload as well as an instance of the
    // socket to communicate with the server
    let handle_test = |payload: Payload, socket: RawClient| {
        match payload {
            Payload::Text(text, _) => println!("Received json: {:#?}", text),
            Payload::Binary(bin_data, _) => println!("Received bytes: {:#?}", bin_data),
            #[allow(deprecated)]
            // Use Payload::Text instead
            Payload::String(str, _) => println!("Received string: {}", str),
        }
        socket
            .emit("test", json!({"got ack": true}))
            .expect("Server unreachable")
    };

    // get a socket that is connected to the admin namespace
    let socket = ClientBuilder::new("http://localhost:4200")
        .namespace("/admin")
        // Saved closure
        .on("test", handle_test)
        // Inline closure
        .on("error", |err, _| eprintln!("Error: {:#?}", err))
        // Function call with signature (payload: Payload, socket: RawClient) -> ()
        .on("foo", handle_foo)
        // Reserved event names are case insensitive
        .on("oPeN", |_, _| println!("Connected"))
        // Custom names are case sensitive
        .on("Test", |_, _| println!("TesT received"))
        // Event specified by enum
        .on(Event::Close, |_, socket| {
            println!("Socket Closed");
            socket
                .emit("message", json!({"foo": "Hello server"}))
                .expect("Error emitting");
        })
        .connect()
        .expect("Connection failed");

    // use the socket

    socket.disconnect().expect("Disconnect failed")
}