subscribe_tcp/
subscribe_tcp.rs

1// define the on_message function (callback).
2pub fn on_msg(topic: String, message: Vec<u8>) {
3    println!("topic: {} message: {:?}", topic, message)
4}
5#[tokio::main]
6async fn main() -> Result<(), String> {
7    let client_type = simple_pub_sub::client::PubSubTcpClient {
8        server: "localhost".to_string(),
9        port: 6480,
10        cert: None,
11        cert_password: None,
12    };
13
14    // initialize the client.
15    let mut client =
16        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
17    // set the callback function.
18    client.on_message(on_msg);
19    // connect the client.
20    let _ = client.connect().await;
21    // subscribe to the given topic.
22    let _ = client.subscribe("abc".to_string()).await;
23    Ok(())
24}