presentation: width: 1024
type MessageHandler = Box<dyn FnMut(&[u8]) -> std::result::Result<(), ()> + Sync + Send>;
//#[derive(Debug)]
pub struct Client {
addr: String,
writer: Arc<Mutex<WriteHalf<TcpStream>>>,
pub stop: Option<oneshot::Sender<()>>,
sid: u64,
handler: Arc<Mutex<HashMap<String, MessageHandler>>>,
}
启动后台Client读取消息任务
pub async fn connect(addr: &str) -> std::io::Result<Client> {}
向服务器发布一条pub消息
pub async fn pub_message(&mut self,
subject: &str,
msg: &[u8])
-> std::io::Result<()> {}
向服务器发布一条sub消息 然后等待服务器推送相关消息
//sub消息格式为SUB subject {queue} {sid}\r\n
//可能由于rustc的bug,导致如果subject是&str,则会报错E0700,暂时使用String来替代
pub async fn sub_message(
&mut self,
subject: String,
queue: Option<String>,
handler: MessageHandler,
) -> std::io::Result<()> {}
分派给相应的Subscribe
/*
从服务器接收pub消息
然后推送给相关的订阅方。
*/
async fn receive_task(
mut reader: ReadHalf<TcpStream>,
stop: oneshot::Receiver<()>,
handler: Arc<Mutex<HashMap<String, MessageHandler>>>,
writer: Arc<Mutex<WriteHalf<TcpStream>>>,
) {}
c.pub_message("test", format!("hello{}", i).as_bytes())
.await?;
c.sub_message(
"test".into(),
None,
Box::new(move |msg| {
println!("recevied:{}", unsafe { std::str::from_utf8_unchecked(msg) });
Ok(())
}),
)
.await?;