Struct simple_pub_sub::client::Client

source ·
pub struct Client {
    pub client_type: PubSubClient,
    /* private fields */
}
Expand description

Simple pub sub Client

Fields§

§client_type: PubSubClient

Implementations§

source§

impl Client

source

pub fn new(client_type: PubSubClient) -> Client

Creates a new instance of Client

Examples found in repository?
examples/subscribe_unix.rs (lines 11-13)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubUnixClient {
        path: "/tmp/simple.sock".to_string(),
    };
    // initialize the client.
    let mut client = simple_pub_sub::client::Client::new(
        simple_pub_sub::client::PubSubClient::Unix(client_type),
    );
    // set the callback function.
    client.on_message(on_msg);
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client.subscribe("abc".to_string()).await;
    Ok(())
}
More examples
Hide additional examples
examples/publish_unix.rs (lines 7-9)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubUnixClient {
        path: "/tmp/simple.sock".to_string(),
    };
    // initialize the client.
    let mut client = simple_pub_sub::client::Client::new(
        simple_pub_sub::client::PubSubClient::Unix(client_type),
    );
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client
        .publish(
            "abc".to_string(),
            "test message".to_string().into_bytes().to_vec(),
        )
        .await;
    Ok(())
}
examples/subscribe_tcp.rs (line 16)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
    };

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

    // initialize the client.
    let mut client =
        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client
        .publish(
            "abc".to_string(),
            "test message".to_string().into_bytes().to_vec(),
        )
        .await;
    Ok(())
}
source

pub fn on_message(&mut self, callback: fn(_: String, _: Vec<u8>))

Sets the on_message callback function

Examples found in repository?
examples/subscribe_unix.rs (line 15)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubUnixClient {
        path: "/tmp/simple.sock".to_string(),
    };
    // initialize the client.
    let mut client = simple_pub_sub::client::Client::new(
        simple_pub_sub::client::PubSubClient::Unix(client_type),
    );
    // set the callback function.
    client.on_message(on_msg);
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client.subscribe("abc".to_string()).await;
    Ok(())
}
More examples
Hide additional examples
examples/subscribe_tcp.rs (line 18)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
    };

    // initialize the client.
    let mut client =
        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
    // set the callback function.
    client.on_message(on_msg);
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client.subscribe("abc".to_string()).await;
    Ok(())
}
source

pub async fn connect(&mut self) -> Result<(), Error>

Connects to the server

Examples found in repository?
examples/subscribe_unix.rs (line 17)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubUnixClient {
        path: "/tmp/simple.sock".to_string(),
    };
    // initialize the client.
    let mut client = simple_pub_sub::client::Client::new(
        simple_pub_sub::client::PubSubClient::Unix(client_type),
    );
    // set the callback function.
    client.on_message(on_msg);
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client.subscribe("abc".to_string()).await;
    Ok(())
}
More examples
Hide additional examples
examples/publish_unix.rs (line 11)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubUnixClient {
        path: "/tmp/simple.sock".to_string(),
    };
    // initialize the client.
    let mut client = simple_pub_sub::client::Client::new(
        simple_pub_sub::client::PubSubClient::Unix(client_type),
    );
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client
        .publish(
            "abc".to_string(),
            "test message".to_string().into_bytes().to_vec(),
        )
        .await;
    Ok(())
}
examples/subscribe_tcp.rs (line 20)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
    };

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

    // initialize the client.
    let mut client =
        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client
        .publish(
            "abc".to_string(),
            "test message".to_string().into_bytes().to_vec(),
        )
        .await;
    Ok(())
}
source

pub async fn post(&mut self, msg: Msg) -> Result<Vec<u8>, Error>

Sends the message to the given server and returns the ack the server could be either a tcp or unix server

source

pub async fn publish( &mut self, topic: String, message: Vec<u8>, ) -> Result<(), Error>

Publishes the message to the given topic

Examples found in repository?
examples/publish_unix.rs (lines 14-17)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubUnixClient {
        path: "/tmp/simple.sock".to_string(),
    };
    // initialize the client.
    let mut client = simple_pub_sub::client::Client::new(
        simple_pub_sub::client::PubSubClient::Unix(client_type),
    );
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client
        .publish(
            "abc".to_string(),
            "test message".to_string().into_bytes().to_vec(),
        )
        .await;
    Ok(())
}
More examples
Hide additional examples
examples/publish_tcp.rs (lines 17-20)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
    };

    // initialize the client.
    let mut client =
        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client
        .publish(
            "abc".to_string(),
            "test message".to_string().into_bytes().to_vec(),
        )
        .await;
    Ok(())
}
source

pub async fn query(&mut self, topic: String) -> Result<String, Error>

Sends the query message to the server

source

pub async fn subscribe(&mut self, topic: String) -> Result<(), Error>

subscribes to the given topic

Examples found in repository?
examples/subscribe_unix.rs (line 19)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubUnixClient {
        path: "/tmp/simple.sock".to_string(),
    };
    // initialize the client.
    let mut client = simple_pub_sub::client::Client::new(
        simple_pub_sub::client::PubSubClient::Unix(client_type),
    );
    // set the callback function.
    client.on_message(on_msg);
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client.subscribe("abc".to_string()).await;
    Ok(())
}
More examples
Hide additional examples
examples/subscribe_tcp.rs (line 22)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
    };

    // initialize the client.
    let mut client =
        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
    // set the callback function.
    client.on_message(on_msg);
    // connect the client.
    let _ = client.connect().await;
    // subscribe to the given topic.
    let _ = client.subscribe("abc".to_string()).await;
    Ok(())
}
source

pub async fn write(&mut self, message: Vec<u8>) -> Result<(), Error>

source

pub async fn read(&mut self, message: &mut [u8]) -> Result<(), Error>

source

pub async fn read_buf(&mut self, message: &mut Vec<u8>) -> Result<(), Error>

source

pub async fn read_message(&mut self) -> Result<Msg, Error>

Trait Implementations§

source§

impl Debug for Client

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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, U> Into<U> for T
where 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, U> TryFrom<U> for T
where 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 T
where 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.