zbus_lib/wsocket/
handler.rs

1use std::sync::mpsc::{channel, Sender};
2use std::time::Duration;
3
4use crate::err::{OkResult, ZbusErr};
5use crate::message::{Message, Request, Response};
6use crate::wsocket::Instruct;
7
8//对wsclient的包装,可以跨线程使用客户端
9pub struct WsClientHandler {
10    pub tx: Sender<Instruct>,
11}
12
13impl WsClientHandler {
14    pub fn is_close(&self) -> OkResult {
15        Ok(())
16    }
17    pub fn response(&self, msg_id: String) -> Result<Response, ZbusErr> {
18        let (tx, rx) = channel();
19        self.excute(Instruct::Response(msg_id, Some(tx)))?;
20        rx.recv_timeout(Duration::from_millis(500))?
21    }
22    pub fn send(&self, msg: Message) -> OkResult {
23        let (tx, rx) = channel();
24        self.excute(Instruct::Delivery(msg, Some(tx)))?;
25        rx.recv_timeout(Duration::from_secs(10))?
26    }
27    fn excute(&self, instruct: Instruct) -> OkResult {
28        self.tx.send(instruct).map_err(|e| ZbusErr::err(e.to_string()))
29    }
30
31// fn close(&self)->Result<(),ZbusErr>;
32}