pub struct Client {
pub client_type: PubSubClient,
/* private fields */
}Expand description
Simple pub sub Client
Fields§
§client_type: PubSubClientImplementations§
Source§impl Client
impl Client
Sourcepub fn new(client_type: PubSubClient) -> Self
pub fn new(client_type: PubSubClient) -> Self
Creates a new instance of Client
use simple_pub_sub::client::{self, PubSubClient, Client};
let client_type = simple_pub_sub::client::PubSubTcpClient {
server: "localhost".to_string(),
port: 6480,
cert: None,
cert_password: None,
};
// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
simple_pub_sub::client::PubSubClient::Tcp(client_type)
);Examples found in repository?
examples/publish_unix.rs (lines 7-9)
2async fn main() -> Result<(), anyhow::Error> {
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
11 client.connect().await?;
12 // publish to the given topic.
13 client
14 .publish(
15 "abc".to_string(),
16 "test message".to_string().into_bytes().to_vec(),
17 )
18 .await
19}More examples
examples/publish_tcp.rs (line 11)
2async fn main() -> Result<(), anyhow::Error> {
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 // initialize the client.
10 let mut client =
11 simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
12
13 client.connect().await?;
14 // publish to the given topic.
15 client
16 .publish(
17 "abc".to_string(),
18 "test message".to_string().into_bytes().to_vec(),
19 )
20 .await
21}examples/subscribe_unix.rs (lines 9-11)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubUnixClient {
6 path: "/tmp/simple.sock".to_string(),
7 };
8 // initialize the client.
9 let mut client = simple_pub_sub::client::Client::new(
10 simple_pub_sub::client::PubSubClient::Unix(client_type),
11 );
12
13 // connect the client.
14 client.connect().await?;
15 // subscribe to the given topic.
16 client.subscribe("abc".to_string()).await?;
17
18 loop {
19 select! {
20 msg = client.read_message()=>{
21 match msg{
22 Ok(msg)=>{
23 println!("{}:{:?}", msg.topic, msg.message);
24 }
25 Err(e)=>{
26 println!("Error: {:?}", e);
27 }
28 }
29 }
30 }
31 }
32}examples/subscribe_tcp.rs (line 14)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubTcpClient {
6 server: "localhost".to_string(),
7 port: 6480,
8 cert: None,
9 cert_password: None,
10 };
11
12 // initialize the client.
13 let mut client =
14 simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16 client.connect().await?;
17 // subscribe to the given topic.
18 client.subscribe("abc".to_string()).await?;
19
20 loop {
21 select! {
22 msg = client.read_message()=>{
23 match msg{
24 Ok(msg)=>{
25 println!("{}:{:?}", msg.topic, msg.message);
26 }
27 Err(e)=>{
28 println!("Error: {:?}", e);
29 }
30 }
31 }
32 }
33 }
34}Sourcepub async fn connect(&mut self) -> Result<()>
pub async fn connect(&mut self) -> Result<()>
Connects to the server
use simple_pub_sub::client::{self, PubSubClient, Client};
let client_type = simple_pub_sub::client::PubSubTcpClient {
server: "localhost".to_string(),
port: 6480,
cert: None,
cert_password: None,
};
// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
simple_pub_sub::client::PubSubClient::Tcp(client_type),
);
pub_sub_client.connect();Examples found in repository?
examples/publish_unix.rs (line 11)
2async fn main() -> Result<(), anyhow::Error> {
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
11 client.connect().await?;
12 // publish to the given topic.
13 client
14 .publish(
15 "abc".to_string(),
16 "test message".to_string().into_bytes().to_vec(),
17 )
18 .await
19}More examples
examples/publish_tcp.rs (line 13)
2async fn main() -> Result<(), anyhow::Error> {
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 // initialize the client.
10 let mut client =
11 simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
12
13 client.connect().await?;
14 // publish to the given topic.
15 client
16 .publish(
17 "abc".to_string(),
18 "test message".to_string().into_bytes().to_vec(),
19 )
20 .await
21}examples/subscribe_unix.rs (line 14)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubUnixClient {
6 path: "/tmp/simple.sock".to_string(),
7 };
8 // initialize the client.
9 let mut client = simple_pub_sub::client::Client::new(
10 simple_pub_sub::client::PubSubClient::Unix(client_type),
11 );
12
13 // connect the client.
14 client.connect().await?;
15 // subscribe to the given topic.
16 client.subscribe("abc".to_string()).await?;
17
18 loop {
19 select! {
20 msg = client.read_message()=>{
21 match msg{
22 Ok(msg)=>{
23 println!("{}:{:?}", msg.topic, msg.message);
24 }
25 Err(e)=>{
26 println!("Error: {:?}", e);
27 }
28 }
29 }
30 }
31 }
32}examples/subscribe_tcp.rs (line 16)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubTcpClient {
6 server: "localhost".to_string(),
7 port: 6480,
8 cert: None,
9 cert_password: None,
10 };
11
12 // initialize the client.
13 let mut client =
14 simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16 client.connect().await?;
17 // subscribe to the given topic.
18 client.subscribe("abc".to_string()).await?;
19
20 loop {
21 select! {
22 msg = client.read_message()=>{
23 match msg{
24 Ok(msg)=>{
25 println!("{}:{:?}", msg.topic, msg.message);
26 }
27 Err(e)=>{
28 println!("Error: {:?}", e);
29 }
30 }
31 }
32 }
33 }
34}Sourcepub async fn post(&mut self, msg: Msg) -> Result<Vec<u8>>
pub async fn post(&mut self, msg: Msg) -> Result<Vec<u8>>
Sends the message to the given server and returns the ack the server could be either a tcp or unix server
use simple_pub_sub::client::{PubSubClient, Client};
use simple_pub_sub::message::Msg;
use simple_pub_sub::PktType;
async fn publish_msg(){
let client_type = simple_pub_sub::client::PubSubTcpClient {
server: "localhost".to_string(),
port: 6480,
cert: None,
cert_password: None,
};
// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
simple_pub_sub::client::PubSubClient::Tcp(client_type),
);
pub_sub_client.connect().await.unwrap();
let msg = Msg::new(PktType::PUBLISH, "Test".to_string(), Some(b"The message".to_vec()));
pub_sub_client.post(msg).await.unwrap();
}Sourcepub async fn publish(&mut self, topic: String, message: Vec<u8>) -> Result<()>
pub async fn publish(&mut self, topic: String, message: Vec<u8>) -> Result<()>
Publishes the message to the given topic
use simple_pub_sub::client::{PubSubClient, Client};
async fn publish_msg(){
let client_type = simple_pub_sub::client::PubSubTcpClient {
server: "localhost".to_string(),
port: 6480,
cert: None,
cert_password: None,
};
// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
simple_pub_sub::client::PubSubClient::Tcp(client_type),
);
pub_sub_client.connect().await.unwrap();
// subscribe to the given topic.
pub_sub_client
.publish(
"Abc".to_string(),
"Test message".to_string().into_bytes().to_vec(),
).await.unwrap();
}Examples found in repository?
examples/publish_unix.rs (lines 14-17)
2async fn main() -> Result<(), anyhow::Error> {
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
11 client.connect().await?;
12 // publish to the given topic.
13 client
14 .publish(
15 "abc".to_string(),
16 "test message".to_string().into_bytes().to_vec(),
17 )
18 .await
19}More examples
examples/publish_tcp.rs (lines 16-19)
2async fn main() -> Result<(), anyhow::Error> {
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 // initialize the client.
10 let mut client =
11 simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
12
13 client.connect().await?;
14 // publish to the given topic.
15 client
16 .publish(
17 "abc".to_string(),
18 "test message".to_string().into_bytes().to_vec(),
19 )
20 .await
21}Sourcepub async fn query(&mut self, topic: String) -> Result<String>
pub async fn query(&mut self, topic: String) -> Result<String>
Sends the query message to the server
use simple_pub_sub::client::{self, PubSubClient, Client};
async fn query(){
let client_type = simple_pub_sub::client::PubSubTcpClient {
server: "localhost".to_string(),
port: 6480,
cert: None,
cert_password: None,
};
// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
simple_pub_sub::client::PubSubClient::Tcp(client_type),
);
pub_sub_client.connect().await.unwrap();
pub_sub_client.query("Test".to_string());
}Sourcepub async fn subscribe(&mut self, topic: String) -> Result<()>
pub async fn subscribe(&mut self, topic: String) -> Result<()>
subscribes to the given topic
use simple_pub_sub::client::{self, PubSubClient, Client};
let client_type = simple_pub_sub::client::PubSubTcpClient {
server: "localhost".to_string(),
port: 6480,
cert: None,
cert_password: None,
};
// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
simple_pub_sub::client::PubSubClient::Tcp(client_type));
pub_sub_client.subscribe("Test".to_string());Examples found in repository?
examples/subscribe_unix.rs (line 16)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubUnixClient {
6 path: "/tmp/simple.sock".to_string(),
7 };
8 // initialize the client.
9 let mut client = simple_pub_sub::client::Client::new(
10 simple_pub_sub::client::PubSubClient::Unix(client_type),
11 );
12
13 // connect the client.
14 client.connect().await?;
15 // subscribe to the given topic.
16 client.subscribe("abc".to_string()).await?;
17
18 loop {
19 select! {
20 msg = client.read_message()=>{
21 match msg{
22 Ok(msg)=>{
23 println!("{}:{:?}", msg.topic, msg.message);
24 }
25 Err(e)=>{
26 println!("Error: {:?}", e);
27 }
28 }
29 }
30 }
31 }
32}More examples
examples/subscribe_tcp.rs (line 18)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubTcpClient {
6 server: "localhost".to_string(),
7 port: 6480,
8 cert: None,
9 cert_password: None,
10 };
11
12 // initialize the client.
13 let mut client =
14 simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16 client.connect().await?;
17 // subscribe to the given topic.
18 client.subscribe("abc".to_string()).await?;
19
20 loop {
21 select! {
22 msg = client.read_message()=>{
23 match msg{
24 Ok(msg)=>{
25 println!("{}:{:?}", msg.topic, msg.message);
26 }
27 Err(e)=>{
28 println!("Error: {:?}", e);
29 }
30 }
31 }
32 }
33 }
34}Sourcepub async fn read_message(&mut self) -> Result<Msg>
pub async fn read_message(&mut self) -> Result<Msg>
reads the incoming message from the server useful when you need to read the messages in loop
use simple_pub_sub::client::{self, PubSubClient, Client};
async fn read_messages(){
let client_type = simple_pub_sub::client::PubSubTcpClient {
server: "localhost".to_string(),
port: 6480,
cert: None,
cert_password: None,
};
// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
simple_pub_sub::client::PubSubClient::Tcp(client_type));
pub_sub_client.connect().await.unwrap();
pub_sub_client.subscribe("Test".to_string()).await.unwrap();
loop {
match pub_sub_client.read_message().await{
Ok(msg)=>{
println!("{}: {:?}", msg.topic, msg.message);
}
Err(e)=>{
println!("error: {:?}", e);
break
}
}
}
}Examples found in repository?
examples/subscribe_unix.rs (line 20)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubUnixClient {
6 path: "/tmp/simple.sock".to_string(),
7 };
8 // initialize the client.
9 let mut client = simple_pub_sub::client::Client::new(
10 simple_pub_sub::client::PubSubClient::Unix(client_type),
11 );
12
13 // connect the client.
14 client.connect().await?;
15 // subscribe to the given topic.
16 client.subscribe("abc".to_string()).await?;
17
18 loop {
19 select! {
20 msg = client.read_message()=>{
21 match msg{
22 Ok(msg)=>{
23 println!("{}:{:?}", msg.topic, msg.message);
24 }
25 Err(e)=>{
26 println!("Error: {:?}", e);
27 }
28 }
29 }
30 }
31 }
32}More examples
examples/subscribe_tcp.rs (line 22)
4async fn main() -> Result<(), anyhow::Error> {
5 let client_type = simple_pub_sub::client::PubSubTcpClient {
6 server: "localhost".to_string(),
7 port: 6480,
8 cert: None,
9 cert_password: None,
10 };
11
12 // initialize the client.
13 let mut client =
14 simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16 client.connect().await?;
17 // subscribe to the given topic.
18 client.subscribe("abc".to_string()).await?;
19
20 loop {
21 select! {
22 msg = client.read_message()=>{
23 match msg{
24 Ok(msg)=>{
25 println!("{}:{:?}", msg.topic, msg.message);
26 }
27 Err(e)=>{
28 println!("Error: {:?}", e);
29 }
30 }
31 }
32 }
33 }
34}Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more