7个版本
0.0.8 | 2022年3月25日 |
---|---|
0.0.7 | 2022年3月25日 |
0.0.6 | 2021年9月11日 |
0.0.5 | 2021年8月19日 |
0.0.2 | 2021年7月31日 |
在#stomp中排名5
每月238次下载
45KB
1K SLoC
stomp-rs
客户端
创建新连接
let client = Client::connect(
ClientBuilder::new("127.0.0.1:61613")
).await?;
订阅
let (sender, mut receiver) = channel(16);
tokio::spawn(async move {
match receiver.recv().await {
Some(frame) => { /* process frame */}
None => { }
}
});
client.subscribe(
Subscribe::new_with_random_id("/topic/test"),
sender
).await
发送
client.send(
Send::new("/topic/test")
.body("test-message")
).await
事务
let transaction = client.begin().await?;
transaction.send(
Send::new("/topic/test")
.body("test-message")
).await
lib.rs
:
Stomp库
使用方法
创建客户端
use stomp_rs::client::{Client, ClientBuilder};
use tokio::net::TcpStream;
use tokio::sync::mpsc::channel;
use std::error::Error;
async fn connect() -> Result<Client, Box<dyn Error>> {
Client::connect(
ClientBuilder::new("127.0.0.1:61613")
).await
}
发射新帧
use stomp_rs::protocol::frame::Send;
use stomp_rs::client::Client;
use std::error::Error;;
async fn send_example(client: &Client) -> Result<(), Box<dyn Error>> {
client.send(
Send::new("/topic/test")
.body("test-message")
).await
}
订阅
use stomp_rs::client::Client;
use stomp_rs::protocol::frame::Subscribe;
use tokio::sync::mpsc::{channel, Sender, Receiver};
use std::error::Error;
use stomp_rs::protocol::{Frame, ServerCommand};
use std::future::Future;
use std::sync::Arc;
async fn subscribe_example(client: Arc<Client>)-> Result<(), Box<dyn Error>> {
let (sender, mut receiver): (Sender<Frame<ServerCommand>>, Receiver<Frame<ServerCommand>>) = channel(16);
let subscriber_client = Arc::clone(&client);
tokio::spawn(async move {
match receiver.recv().await {
Some(frame) => {
/* process frame */
// Send ack to server
subscriber_client.ack(frame.ack().unwrap())
.await;
}
None => { }
}
});
client.subscribe(
Subscribe::new_with_random_id("/topic/test"),
sender
).await
}
依赖项
~3–9.5MB
~83K SLoC