#handler #process #dev #websocket-client #tokio-tungstenite #ctrl-c

bin+lib wsclient

A websocket client using tokio-tungstenite for easy dev

3 releases

0.1.2 Apr 17, 2023
0.1.1 Apr 17, 2023
0.1.0 Apr 17, 2023

#301 in WebSocket

41 downloads per month

MIT/Apache

10KB
122 lines

Usage

use ctrlc;
use std::env;
use wsclient;

#[derive(Default)]
struct DataListener {}

impl wsclient::Handler for DataListener {
    fn process(&self, data: String) {
        println!("{:?}", data);
    }
}

#[tokio::main]
pub async fn main() {
    let url = env::args()
        .nth(1)
        .unwrap_or_else(|| panic!("this program requires at least one argument"));
    let listener = DataListener::default();
    wsclient::WsClient::new()
        .start(url, Box::new(listener))
        .await;

    use std::sync::mpsc::channel;
    let (tx, rx) = channel();
    ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
        .expect("Error setting Ctrl-C handler");
    rx.recv().expect("Could not receive from channel.");
    println!("Ctrl-C and exiting...");
    std::process::exit(0);
}

Dependencies

~6–16MB
~190K SLoC