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

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

Connects to the server

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

Source§

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>,

Source§

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.